我似乎无法获得我需要的价值,我意识到它可能很简单,但在这里有点时间。
{
"kind": "some#sourceListResponse",
"etag": "\"DsOZ7qVJA4erererererererE6ck/0wSdsdfsddfsdfewrer\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [{
"kind": "some#source",
"etag": "\"DsOZ7qVhdjfgsjfkdjfklsdjfkdkfdkldjfsdf\"",
"id": "UCN8d8d8d8d8d8d8d8d8d8d8d8dd",
"contentDetails": {
"relatedPlaylists": {
"likes": "maryjosephdavidhenrylisa",
"uploads": "lisahenrydavidjosephmary"
},
"UserId": "1234567890"
}
}]
}
我需要解析"项目的列表/数组"包括对象中的每个字段和contentDetails对象值,包括UserID。
if (!messageNode.isArray() && !messageNode.isObject()){
try {
throw new Exception("INVALID JSON");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else if(messageNode.isObject()) {
ObjectMapper m = new ObjectMapper();
JsonNode rootNode = m.readTree(fetchResult);
JsonNode array = rootNode.get("items");
if(array.size() == 0){
Text = "DOES NOT EXIST!";
}
else{
//JsonNode jsonNode = array.get("id");
String aText = array.toString();
JsonNode rNode = m.readTree(aText);
JsonNode nameNode = rootNode.path("id");
pageID=nameNode.textValue();
我能够获得物品清单,但我正在努力争取这些领域。任何帮助赞赏。
编辑:
得到以下工作。将进一步测试
if (array.isArray()) {
for (final JsonNode objNode : array) {
System.out.println(objNode);
pageID=objNode.get("id").toString();
}
}
答案 0 :(得分:0)
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(new File("json.txt"));
JsonNode items = root.get("items");
for (JsonNode node: items) {
String kind = node.get("kind").asText();
String etag = node.get("etag").asText();
String id = node.get("id").asText();
JsonNode contentDetails = node.get("contentDetails");
JsonNode relatedPlaylists = node.get("contentDetails").get("relatedPlaylists");
String likes = relatedPlaylists.get("likes").asText();
String uploads = relatedPlaylists.get("uploads").asText();
String userId = contentDetails.get("UserId").asText();
}