0

Im unable to access data from nested objects from the inner elements. This is the json string-

 {"created_at":"Tue Jun 14 04:06:55 +0000       2016",
"id":7426,
"id_str":"7425",
"text":"sample",
"user":{"id":529094887,"id_str":"529094887","name":"One Direction Japan","screen_name":"1D_OfficialJP"}
"entities":{"hashtags":[{"text":"ChoiceLoveSong"},{"text":"1DJP"],"media_url":"http:\/\/pbs.twimg.com\/media\/Ck4ijC2UkAA59rU.jpg",]}}

This is my code-

try{        JSONParser parser = new JSONParser();
            JSONObject jsonObject = (JSONObject) parser.parse(jsontext);

            createdat = (String) jsonObject.get("created_at");
            System.out.println(createdat);

            twittertext = (String) jsonObject.get("text");
            System.out.println(twittertext);

            if (jsonObject.get("id")!= null)
            id = (long) jsonObject.get("id");
            System.out.println(id);

            id_str = (String) jsonObject.get("id_str");
            System.out.println(id_str);

          // loop array

            JsonNode json = new ObjectMapper().readTree(jsontext);
           JsonNode user_fields = json.get("user");

           name = user_fields.get("name").asText();
           System.out.println(name);
           scrname=user_fields.get("screen_name").asText();
           System.out.println(scrname);

          JsonNode entities_fields = json.get("entities");
         String hashtags = entities_fields.get("hashtags").asText();
         System.out.println("hashtags is "+ hashtags);
         JSONArray hashtagsContent = (JSONArray) jsonObject.get("hashtags");
         Iterator<String> entitiesNames = entities_fields.getFieldNames();

                     while (entitiesNames.hasNext()) {
                         System.out.println(entitiesNames.next());     
                          } 

                    jsonObject.get("user_mentions");

                   }

// followed by catch block ....

Actually im interested in extracting data from inside the hashtag array, specifically i want to store those datas in an array.With the above code im only able to print the keys and not the values of entities object. Also the number of elements in the array may vary

Romit Kumar
  • 2,442
  • 6
  • 21
  • 34

0 Answers0