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

6
  • format your code please :) Commented Jun 29, 2016 at 5:39
  • Take a look at this Commented Jun 29, 2016 at 5:44
  • Check now @JordiCastilla Commented Jun 29, 2016 at 5:48
  • Thank you @AbdullahAhçı I dont know why the same code given in the link did not work however using that concept this worked for me- JSONObject getObject = (JSONObject) jsonObject.get("entities"); JSONArray getArray =(JSONArray) getObject.get("hashtags"); for(int j = 0; j < getArray.size(); j++) { JSONObject objects = (JSONObject) getArray.get(j); String test =(String) objects.get("text"); System.out.println(test); } Commented Jun 29, 2016 at 6:11
  • sorry for improper format..im new to stackoverflow Commented Jun 29, 2016 at 6:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.