Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parsing json array field using jackson in java

Tags:

java

json

jackson

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.

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

                        }
                    }
like image 953
vbNewbie Avatar asked Nov 26 '25 04:11

vbNewbie


1 Answers

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

    }
like image 62
sparth Avatar answered Nov 28 '25 17:11

sparth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!