Question:
Is there a concise way to convert a JSON string into an object representation in Java ME? The conventional method requires multiple lines of tedious coding, as shown below:
Object n = create("new"); setString(p, "name", "MyNode"); setInteger(p, "width", 200); setInteger(p, "height", 100);
Answer:
Consider utilizing external libraries for enhanced JSON handling capabilities. One highly recommended library for Java ME is:
http://code.google.com/p/json-simple/
This lightweight library makes JSON parsing efficient and effortless, enabling you to convert a JSON string into an object in a single line of code:
JSONObject json = (JSONObject)new JSONParser().parse("{\"name\":\"MyNode\", \"width\":200, \"height\":100}");
You can then retrieve specific data fields from the parsed object:
System.out.println("name=" + json.get("name")); System.out.println("width=" + json.get("width"));
The above is the detailed content of How Can I Efficiently Parse JSON Strings into Objects in Java ME?. For more information, please follow other related articles on the PHP Chinese website!