Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java Play中解析JSON_Java_Json_Playframework_Playframework 2.0 - Fatal编程技术网

在Java Play中解析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\

我想在游戏中解析一个简单的JSON对象,我目前正在尝试以下操作,但运气不佳:

    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 表单中,所以你可能要考虑制作<代码>地图<代码>,而不是<代码> MAP<代码>,如果是这样的话。

< P> 2使用Jackson API作为JSON,所以你应该使用它< /P> 样本:

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")
        );
    }
}