javajson-Jackson-Nested元素

javajson-Jackson-Nested元素,java,json,jackson,Java,Json,Jackson,我的JSON字符串具有嵌套值 差不多 “[{”列出的计数:1720,“状态:{”转发计数:78}]” 我想要retweet\u计数的值 我在用杰克逊 下面的代码输出“{retweet\u count=78}”而不是78。我想知道我是否可以像PHP那样获得嵌套值,即status->retweet\u count。谢谢 import java.io.IOException; import java.util.List; import java.util.Map; import org.codehau

我的JSON字符串具有嵌套值

差不多

“[{”列出的计数:1720,“状态:{”转发计数:78}]”

我想要
retweet\u计数的值

我在用杰克逊

下面的代码输出“
{retweet\u count=78}
”而不是
78
。我想知道我是否可以像PHP那样获得嵌套值,即
status->retweet\u count
。谢谢

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

    }
}
import java.io.IOException;
导入java.util.List;
导入java.util.Map;
导入org.codehaus.jackson.map.ObjectMapper;
导入org.codehaus.jackson.type.TypeReference;
公开课考试{
公共静态void main(字符串[]args)引发IOException{
ObjectMapper mapper=新的ObjectMapper();
List fwers=mapper.readValue(“[{\”列出的计数\“:1720,\”状态\“:{\”转发计数\“:78}}]]”,新类型引用(){});
System.out.println(fwers.get(0.get)(“状态”);
}
}

您可能可以执行
System.out.println(fwers.get(0).get(“status”).get(“retweet\u count”)

编辑1:

改变

List <Map<String, Object>> fwers = mapper.readValue(..., new TypeReference<List <Map<String, Object>>>() {});

如果您知道要检索的数据的基本结构,那么正确地表示它是有意义的。你会得到各种各样的细节,比如类型安全;)

公共静态类TweetThingy{
公共国际单位计数;
公众地位;
公共静态类状态{
公共互联网转发次数;
}
}
List tt=mapper.readValue(…,new TypeReference(){});
System.out.println(tt.get(0.status.retweet\u count));

试试这样的方法。如果你使用JsonNode,你的生活会更轻松

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

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

我试过了,它不起作用。get(“status”)是一个普通的objectMore errors bro,:
无法反序列化java.util.LinkedHashMap的实例,超出预期的值\u NUMBER\u INT token
:va;“状态”的意思是一张地图,不是吗?您只需使用“retweet\u count”再次调用“get()”。然而,我同意其中一个建议使用
readTree()
来获得
JsonNode
——更容易遍历的回答。
Map m = (Map) fwers.get(0).get("status");
System.out.println(m.get("retweet_count"));
public static class TweetThingy {
    public int listed_count;
    public Status status;

    public static class Status {
        public int retweet_count;
    }
}

List<TweetThingy> tt = mapper.readValue(..., new TypeReference<List<TweetThingy>>() {});
System.out.println(tt.get(0).status.retweet_count);
JsonNode node = mapper.readValue("[{\"listed_count\":1720,\"status\":{\"retweet_count\":78}}]]", JsonNode.class);

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