在 Java ME 中将 JSON 字符串转换为对象?
Java/J2ME中有没有办法转换字符串,如:
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);
也许是 JSON 库?
Maybe a JSON library?
推荐答案
我用了几个,我最喜欢的是,
I used a few of them and my favorite is,
http://code.google.com/p/json-simple/
该库非常小,因此非常适合 J2ME.
The library is very small so it's perfect for J2ME.
您可以像这样在一行中将 JSON 解析为 Java 对象,
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"));
相关文章