简体   繁体   English

在Java中使用杰克逊解析json数组字段

[英]parsing json array field using jackson in java

I just cannot seem to get the value that I need and I realize it may be simple but kind of tied for time here. 我似乎无法获得我所需要的价值,而且我意识到这可能很简单,但在时间上却束手无策。

{
"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"
    }
  }]
  }

I need to parse the list / array of "items" including each field within the object and the contentDetails object values including UserID. 我需要解析“项目”的列表/数组,包括对象内的每个字段以及包括UserID在内的contentDetails对象值。

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

I am able to get the list of items but I am struggling with the fields. 我可以获取项目列表,但是我在字段中苦苦挣扎。 Any help appreciated. 任何帮助表示赞赏。

EDIT: 编辑:

got the following to sort of work. 有以下工作要做。 Will test further 会进一步测试

                   if (array.isArray()) {
                        for (final JsonNode objNode : array) {
                            System.out.println(objNode);
                            pageID=objNode.get("id").toString();

                        }
                    }
    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();

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM