在 Java ME 中将 JSON 字符串转换为对象? [英] Convert a JSON string to object in Java ME?

查看:370
本文介绍了在 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"));

这篇关于在 Java ME 中将 JSON 字符串转换为对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆