javajsonjava-meserialization

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

  • 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"));