【发布时间】:2011-12-05 16:41:29
【问题描述】:
我的 JSON 字符串有嵌套值。
有点像
"[{"listed_count":1720,"status":{"retweet_count":78}}]"
我想要retweet_count 的值。
我正在使用杰克逊。
下面的代码输出“{retweet_count=78}”而不是78。我想知道我是否可以像 PHP 那样获得嵌套值,即status->retweet_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"));
}
}
【问题讨论】:
-
这是意料之中的:'status' 的 va;ue 是一个 Map 不是吗?您只需要使用“retweet_count”再调用一次“get()”。但是,我同意其中一个建议使用
readTree()来获得JsonNode的回复——更容易遍历。