change string to JSON code example

Example 1: Javascript object to JSON string

var person={"first_name":"Tony","last_name":"Hawk","age":31};
var personJSONString=JSON.stringify(person);

Example 2: convert json string to json object in java

try {
     JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
}catch (JSONException err){
     Log.d("Error", err.toString());
}

Example 3: change to json string

var obj =
  { name: "John", today: new Date(), city : "New York" };

  var myJSON = JSON.stringify(obj);

  
document.getElementById("demo").innerHTML = myJSON;