Convert a JSON string to object in Java ME?

Is there a way in Java/J2ME to convert a string, such as:

{name:"MyNode", width:200, height:100}

to an internal Object representation of the same, in one line of code?

Because the current method is too tedious:

Object n = create("new");
setString(p, "name", "MyNode");
setInteger(p, "width", 200);
setInteger(p, "height", 100);

Maybe a JSON library?


Solution 1:

I used a few of them and my favorite is,

http://code.google.com/p/json-simple/

The library is very small so it's perfect for J2ME.

You can parse JSON into Java object in one line like this,

JSONObject json = (JSONObject)new JSONParser().parse("{\"name\":\"MyNode\", \"width\":200, \"height\":100}");
System.out.println("name=" + json.get("name"));
System.out.println("width=" + json.get("width"));

Solution 2:

The simplest option is Jackson:

MyObject ob = new ObjectMapper().readValue(jsonString, MyObject.class);

There are other similarly simple to use libraries (Gson was already mentioned); but some choices are more laborious, like original org.json library, which requires you to create intermediate "JSONObject" even if you have no need for those.

Solution 3:

GSON is a good option to convert java object to json object and vise versa.
It is a tool provided by google.

for converting json to java object use: fromJson(jsonObject,javaclassname.class)
for converting java object to json object use: toJson(javaObject)
and rest will be done automatically

For more information and for download

Solution 4:

You can do this easily with Google GSON.

Let's say you have a class called User with the fields user, width, and height and you want to convert the following json string to the User object.

{"name":"MyNode", "width":200, "height":100}

You can easily do so, without having to cast (keeping nimcap's comment in mind ;) ), with the following code:

Gson gson = new Gson(); 
final User user = gson.fromJson(jsonString, User.class);

Where jsonString is the above JSON String.

For more information, please look into https://code.google.com/p/google-gson/

Solution 5:

You have many JSON parsers for Java:

  • JSONObject.java
    A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get() and opt() methods for accessing the values by name, and put() methods for adding or replacing values by name. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object.

  • JSONArray.java
    A JSONArray is an ordered sequence of values. Its external form is a string wrapped in square brackets with commas between the values. The internal form is an object having get() and opt() methods for accessing the values by index, and put() methods for adding or replacing values. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object.

  • JSONStringer.java
    A JSONStringer is a tool for rapidly producing JSON text.

  • JSONWriter.java
    A JSONWriter is a tool for rapidly writing JSON text to streams.

  • JSONTokener.java
    A JSONTokener takes a source string and extracts characters and tokens from it. It is used by the JSONObject and JSONArray constructors to parse JSON source strings.

  • JSONException.java
    A JSONException is thrown when a syntax or procedural error is detected.

  • JSONString.java
    The JSONString is an interface that allows classes to implement their JSON serialization.