0

I'm following this tutuorial here, and my JSON object is near enough the same, except I have this sort of format:

{"user":{
    "SomeKeys":"SomeValues",
    "SomeList":["val1","val2"]
    }
}

Here is my relevant code:

Object obj = parser.parse(new FileReader("exampleJson.json"));

JSONObject jsonObject = (JSONObject) obj;
JSONObject user = (JSONObject) jsonObject.get("user");
JSONArray list = (JSONArray) user.get("SomeList");

Then my program goes off an gets the values form the keys etc. Or it would but I get a NullPointerException from eclipse. Why is this?

It should unpackage my .json file into jsonObject, unpackage the "user" key as a JSONObject user, then unpackage the "SomeList" key as a JSONArray called list. Except when it does so it must be trying to put one of val1 or val2 into a part of the JSONArray that does not exist and just points into the abyss of null. What have I done to deserve this wrong ?

How do I fix my program?

6
  • Isn't "SomeList":["val1","val2"] just an array instead of a JSONArray Commented Sep 19, 2012 at 16:33
  • I tried your code and it works perfectly for me... Let me know if you want to post the full code of what I tried. Commented Sep 19, 2012 at 16:36
  • Have you tried looking with a debugger? Commented Sep 19, 2012 at 18:04
  • @Joel No, but I will do at work tomorrow. Commented Sep 19, 2012 at 18:27
  • @aymeric That would be helpful, maybe then I can spot where our code has diverged. Commented Sep 19, 2012 at 18:27

2 Answers 2

1

Your code is working well for me

public class App {

    public static void main(final String[] args) throws FileNotFoundException, IOException, ParseException {
        final Object obj = new JSONParser().parse(new FileReader("D:/a.json"));
        final JSONObject jsonObject = (JSONObject) obj;
        final JSONObject user = (JSONObject) jsonObject.get("user");
        final JSONArray list = (JSONArray) user.get("SomeList");
        System.out.println(list);
    }
} 

File D:/exampleJson.json

{"user":{
    "SomeKeys":"SomeValues",
    "SomeList":["val1","val2"]
    }
}  

output is

["val1","val2"]
Sign up to request clarification or add additional context in comments.

1 Comment

Why have you made those objects final?
1

Using the exact same json file:

{"user":{
    "SomeKeys":"SomeValues",
    "SomeList":["val1","val2"]
    }
}

Here is the code I use (with the imports to really make sure we have the same ones):

import java.io.FileReader;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class Test {

    public static void main(String[] args) throws Exception {
        Object obj = new JSONParser().parse(new FileReader("t.json"));

        JSONObject jsonObject = (JSONObject) obj;
        JSONObject user = (JSONObject) jsonObject.get("user");
        JSONArray list = (JSONArray) user.get("SomeList");
        System.out.println(list);
    }
}

And here is the output:

["val1","val2"]

I used maven and m2e to import the library. The version used for this test was json-simple-1.1.jar.

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.