Convert a JSON string to object in Java ME?(在 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 字符串转换为对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java ME 中将 JSON 字符串转换为对象?
基础教程推荐
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
