0

I am trying to parse a json file to extract certain fields using Java. However, I am getting null values for certain fields (tags) for example, country, site_type, replies_count, participants_count etc. I tried to use .toString() after json object but I still keep getting NULL values.

Can anyone please help me to fix the code? As sample of json is as follows:

{
"posts": [
{
  "thread": {
    "uuid": "5cf0a3152a01d082500a46b358e984610ce87c9c",
    "url": "https://www.cnn.com/videos/cnnmoney/2018/05/27/active-shooter-video-game-backlash.hln",
    "site_full": "www.cnn.com",
    "site": "cnn.com",
    "site_section": "http://feeds.feedburner.com/cnn/wYAn",
    "site_categories": [
      "media"
    ],
    "section_title": "CNN.com - RSS Channel - HP Hero",
    "title": "New 'school shooter' game stirs outrage",
    "title_full": "New 'school shooter' game stirs outrage",
    "published": "2018-05-27T15:15:00.000+03:00",
    "replies_count": 0,
    "participants_count": 0,
    "site_type": "news",
    "country": "US",
    "spam_score": 0.0,
    "main_image": "https://cdn.cnn.com/cnnnext/dam/assets/180527080429-video-game-active-shooter-super-tease.jpg",
    "performance_score": 3,
    "domain_rank": 81,
    "social": {
      "facebook": {
        "likes": 399,
        "comments": 0,
        "shares": 399
      },
      "gplus": {
        "shares": 0
      },
      "pinterest": {
        "shares": 2
      },
      "linkedin": {
        "shares": 0
      },
      "stumbledupon": {
        "shares": 0
      },
      "vk": {
        "shares": 0
      }
    }
  },
  "uuid": "5cf0a3152a01d082500a46b358e984610ce87c9c",
  "url": "https://www.cnn.com/videos/cnnmoney/2018/05/27/active-shooter-video-game-backlash.hln",
  "ord_in_thread": 0,
  "author": "",
  "published": "2018-05-27T15:15:00.000+03:00",
  "title": "New 'school shooter' game stirs outrage",
  "text": "MUST WATCH New 'active shooter' video game sparks backlash Thousands of people have petitioned to stop the release of a new video game which takes on the point of view of a school gunman.",
  "highlightText": "",
  "highlightTitle": "",
  "language": "english",
  "external_links": [],
  "entities": {
    "persons": [],
    "organizations": [],
    "locations": []
  },
  "rating": null,
  "crawled": "2018-05-27T16:05:21.021+03:00"
}
],
"total_count": "122"

}

And here is my code (I am using json.simple.* API to parse json):

import java.io.FileReader;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;



public class JSONRetriever {

public static void main(String[] args) {
    JSONParser jsonParser = new JSONParser();
    Object object;

    try {

        object = jsonParser.parse(new FileReader("C:\\sample.json"));
        JSONObject jsonObject = (JSONObject) object;

        JSONArray posts = (JSONArray) jsonObject.get("posts");

        Iterator itr = posts.iterator();

        while (itr.hasNext()) {

            Object slide = itr.next();
            JSONObject jsonObject2 = (JSONObject) slide;
            JSONObject thread = (JSONObject) jsonObject2.get("thread");

            String uuid = (String) thread.get("uuid");
            String url = (String) thread.get("url");
            String site_full = (String) thread.get("site_full");
            String site = (String) thread.get("site");
            String site_section = (String) thread.get("site_section");

            String section_title = (String) jsonObject2.get("section_title");
            String title = (String) jsonObject2.get("title");
            String title_full = (String) jsonObject2.get("title_full");
            String published = (String) jsonObject2.get("published");
            String replies_count = (String) jsonObject2.get("replies_count");
            String participants_count = (String) jsonObject2.get("participants_count");
            String site_type = (String) jsonObject2.get("site_type");
            String country = (String) jsonObject2.get("country");
            String spam_score = (String) jsonObject2.get("spam_score");

            JSONObject social = (JSONObject) jsonObject2.get("social");
            System.out.println("section_title: " + section_title);
            System.out.println("title: " + title);
            System.out.println("title_full: " + title_full);
            System.out.println("published: " + published);
            System.out.println("replies_count: " + replies_count);
            System.out.println("participants_count: " + participants_count);
            System.out.println("site_type: " + site_type);
            System.out.println("country: " + country);
            System.out.println("spam_score: " + spam_score);

            System.out.println("uuid: " + uuid);
            System.out.println("url: " + url);
        }


    }catch(Exception e)
    {
        e.printStackTrace();
    }

}
}

1 Answer 1

1

Your fields are located in your "thread" object, not "jsonObject2", for example use:

 String country = (String) thread.get("country");
Sign up to request clarification or add additional context in comments.

Comments

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.