我的JSON字符串具有嵌套值。Java JSON -Jackson-嵌套元素
喜欢的东西
"[{"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'的方法 - 更容易遍历。 – StaxMan