Java JSON -Jackson-嵌套元素 [英] Java JSON -Jackson- Nested Elements

查看:107
本文介绍了Java JSON -Jackson-嵌套元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSON字符串具有嵌套值。

My JSON string has nested values.

类似

[{listed_count:1720,status :{retweet_count:78}}]

我想要 retweet_count

我正在使用杰克逊。

以下代码输出 {retweet_count = 78} 而不是 78 。我想知道我是否能以PHP的方式获得嵌套值,即 status-> retweet_count 。谢谢。

The code below outputs "{retweet_count=78}" and not 78. I'm wondering if I can get nested values the kind of way PHP does it i.e status->retweet_count. Thanks.

import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class tests {
public static void main(String [] args) throws IOException{
    ObjectMapper mapper = new ObjectMapper();
  List <Map<String, Object>> fwers = mapper.readValue("[{\"listed_count\":1720,\"status\":{\"retweet_count\":78}}]]", new TypeReference<List <Map<String, Object>>>() {});
    System.out.println(fwers.get(0).get("status"));

    }
}


推荐答案

尝试类似的东西。如果你使用JsonNode,你的生活会更容易。

Try something like that. If you use JsonNode your life gonna be easier.

JsonNode node = mapper.readValue("[{\"listed_count\":1720,\"status\":{\"retweet_count\":78}}]]", JsonNode.class);

System.out.println(node.findValues("retweet_count").get(0).asInt());

这篇关于Java JSON -Jackson-嵌套元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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