1

I am a newbie to json parsing, I have grabbed a json string from a request and now I need to parse it with java. I'm using simple-lib for this. But I'm really stuck as I'm not familiar with it. I need to extract following data

I used following java code for that but it's not giving me the result I need, please someone help me...

    JSONParser parser = new JSONParser();

    try {
        Object obj = parser.parse(new FileReader("test.json"));

        JSONObject jsonObject = (JSONObject) obj;

        JSONArray msg = (JSONArray) jsonObject.get("content");
        Iterator<String> iterator = msg.iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next());

        }

Sample JSON

{
    "status": 200,
    "message": "ok",
    "timestamp": "2014-05-22T14:29:56.824+03:00",
    "pagesCount": 1,
    "version": "1.1",
    "pages": [
        {
            "number": 100,
            "subpages": [
                {
                    "number": 1,
                    "timestamp": "2014-05-22T13:41:41.116+03:00",
                    "content": "text"
                },
4
  • 1
    What's the simple lib you are using? Have you tried something more common, like gson or jackson? Commented May 22, 2014 at 12:28
  • @MartinWickman: I think the lib in question is json-simple Commented May 22, 2014 at 12:41
  • What is the result you are hoping to get back ? Are you getting back anything or are you getting an error ? Commented May 22, 2014 at 12:45
  • While there's nothing wrong with other libraries like json-simple per se, I suggest you still take a look at Jackson or Gson, as these are widely used. Commented Aug 29, 2024 at 6:39

3 Answers 3

1

Something like this perhaps?

JSONParser parser = new JSONParser();

JSONObject jsonObject = (JSONObject) parser.parse(new FileReader("test.json"));
JSONArray pages = (JSONArray) jsonObject.get("pages");
if (pages != null) {
    for (Object p : pages) {
        JSONObject page = (JSONObject) p;
        JSONArray subPages = (JSONArray) page.get("subpages");
        if  (subPages != null) {
            for (Object sp : subPages) {
                JSONObject subPage = (JSONObject) sp;
                System.err.println(subPage.get("content"));
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are requesting for the value that corresponds to the key content from your outermost object, but no such key exists in your sample input. In addition, the only field named content has a string as its value and not a JSON array.

To get at the content field you would need to walk the object hierarchy until you reach the element that you need, using something along these lines:

JSONParser parser = new JSONParser();

JSONObject json = (JSONObject) parser.parse(new FileReader("test.json"));
JSONArray pages = (JSONArray) json.get("pages");

// Page 10
JSONObject page = (JSONObject) pages.get(10);

// Subpages of page 10
JSONArray subpages = (JSONArray) page.get("subpages");

// Subpage 7 of page 10
JSONObject subpage = (JSONObject) subpages.get(10);

// Content of subpage 7 of page 10
String content = (String) subpage.get("content");

Please note that I am assuming that e.g. index 10 corresponds to page 10. That may not be true in your case; pages may not be zero-indexed, there may be missing pages or they may not be in the correct order. In that case you will have to iterate over the page and subpage lists and test the value of the number field to find the object that you need.

In any case, if you are indeed using json-simple, as seems to be the case, then JSONObject is a subclass of HashMap and JSONArray a subclass of ArrayList. Therefore, their interfaces should be quite familiar to you.

Disclaimer: Untested code - exception and other error handling removed for brevity

Comments

0

First of all, The json is not valid (if you pasted it complete) In JSON "{}" is an object, and "[]" is an array, others are just key value pairs. Simply you can do like this without using parser ,

JSONObject objResponseJSON = new JSONObject(responseJSONString);
int status = (int) objResponseJSON.getInt("status");
//fetch other
JSONArray pages = objResponseJSON.getJSONArray("pages");
for(int count = 0; count < pages.length(); count++){
   //fetch other
   JSONObject objJSON = pages.getJSONObject(count);
   JSONArray subpages = objJSON.getJSONArray("subpages");
   for(int count1 = 0; count1 < subpages.length(); count1++){
      JSONObject objSubpageJSON = subpages.getJSONObject(count1);
      //fetch other
      int number = (int) objSubpageJSON.getInt("number");
    }
}

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.