0
Q:

how to convert jsonobject to string in java

JSONObject json = new JSONObject();

json.toString();
2
// Imagine we received this text from a web server:
'{ "name":"John", "age":30, "city":"New York"}'

// To convert text into a JavaScript object, use the JavaScript function JSON.parse():
var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');

// OR
var string = '{ "name":"John", "age":30, "city":"New York"}';
var obj = JSON.parse(string);
17
try {
     JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
}catch (JSONException err){
     Log.d("Error", err.toString());
}
1
var json = new JavaScriptSerializer().Serialize(obj);
1
using System;
using System.Web.Script.Serialization;

public class MyDate
{
    public int year;
    public int month;
    public int day;
}

public class Lad
{
    public string firstName;
    public string lastName;
    public MyDate dateOfBirth;
}

class Program
{
    static void Main()
    {
        var obj = new Lad
        {
            firstName = "Markoff",
            lastName = "Chaney",
            dateOfBirth = new MyDate
            {
                year = 1901,
                month = 4,
                day = 30
            }
        };
        var json = new JavaScriptSerializer().Serialize(obj);
        Console.WriteLine(json);
    }
}
0

New to Communities?

Join the community