在Java Play中解析JSON
我想在游戏中解析一个简单的JSON对象,我目前正在尝试以下操作,但运气不佳:在Java Play中解析JSON,java,json,playframework,playframework-2.0,Java,Json,Playframework,Playframework 2.0,我想在游戏中解析一个简单的JSON对象,我目前正在尝试以下操作,但运气不佳: HashMap<String,Object> result = new ObjectMapper().readValue(stringBuilder.toString(), HashMap.class); 有人能提供一个如何解析和迭代的例子吗?看起来您有一个列表,其中每个条目都是一个映射键值对。 您可以使用标准json解析器将其转换为如下对象: String json = "[{\"id\
HashMap<String,Object> result = new ObjectMapper().readValue(stringBuilder.toString(), HashMap.class);
有人能提供一个如何解析和迭代的例子吗?看起来您有一个列表,其中每个条目都是一个映射键值对。
您可以使用标准json解析器将其转换为如下对象:
String json = "[{\"id\":\"537b4f2e30047c51863094dd\",\"from\":\"jacob\",\"to\":\"duncan\",\"subject\":\"Welcome to the message system!\",\"message\":\"Hello World\"},{\"id\":\"537bb23930044f26cfd24464\",\"from\":\"jacob\",\"to\":\"duncan\",\"subject\":\"Welcome to the message system!\",\"message\":\"Hello World\"}]";
Type listType = new TypeToken<List<Map<String, Object>>>(){}.getType();
List<Map<String, Object>> data = new Gson().fromJson(json, listType);
p.S.
看起来你的所有值数据也在
String jsonString = "[{\"id\":\"537b4f2e30047c51863094dd\",\"from\":\"jacob\",\"to\":\"duncan\",\"subject\":\"Welcome to the message system!\",\"message\":\"Hello World\"},{\"id\":\"537bb23930044f26cfd24464\",\"from\":\"jacob\",\"to\":\"duncan\",\"subject\":\"Welcome to the message system!\",\"message\":\"Hello World\"}]";
JsonNode node = Json.parse(jsonString);
if (node.isArray()) {
Iterator<JsonNode> elements = node.elements();
while (elements.hasNext()) {
JsonNode obj = elements.next();
debug(
"Message with ID: " + obj.get("id")
+ " from: " + obj.get("from")
+ " to: " + obj.get("to")
+ " subject: " + obj.get("subject")
+ " message: " + obj.get("message")
);
}
}
String jsonString=“[{\'id\”:\“537b4f2e30047c51863094dd\”,“from\”:“jacob\”,“to\”:“duncan\”,“subject\”:“欢迎使用消息系统!”,“message\”:“Hello World\”,“{\'id\”:“537BB239300F26CFD24464\”,“from\”,“from\:“jacob\”,“to\”:“duncan\”,“subject\”:“欢迎使用消息系统!”,“Hello\”,“message World\”,“欢迎使用消息世界”;“,”;
JsonNode=Json.parse(jsonString);
if(node.isArray()){
迭代器元素=node.elements();
while(elements.hasNext()){
JsonNode obj=elements.next();
调试(
ID为“+obj.get”(“ID”)的消息
+from:“+obj.get”(“from”)
+“to:”+obj.get(“to”)
+“主题:”+obj.get(“主题”)
+“消息:”+obj.get(“消息”)
);
}
}
提示:它是在一段时间前重构的,所以根据所使用的播放版本检查或API这是否适用于您?您到底使用哪个播放版本?
for (Map<String, Object> map : data) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
// do stuff
}
}
String jsonString = "[{\"id\":\"537b4f2e30047c51863094dd\",\"from\":\"jacob\",\"to\":\"duncan\",\"subject\":\"Welcome to the message system!\",\"message\":\"Hello World\"},{\"id\":\"537bb23930044f26cfd24464\",\"from\":\"jacob\",\"to\":\"duncan\",\"subject\":\"Welcome to the message system!\",\"message\":\"Hello World\"}]";
JsonNode node = Json.parse(jsonString);
if (node.isArray()) {
Iterator<JsonNode> elements = node.elements();
while (elements.hasNext()) {
JsonNode obj = elements.next();
debug(
"Message with ID: " + obj.get("id")
+ " from: " + obj.get("from")
+ " to: " + obj.get("to")
+ " subject: " + obj.get("subject")
+ " message: " + obj.get("message")
);
}
}