0

I'm trying to parse this string into java, but I keep getting errors.

{"id":1,"jsonrpc":"2.0","result":{"limits":{"end":3,"start":0,"total":3},"sources":[{"file":"/media/storage/media/re Music/","label":"re Music"},{"file":"/media/storage/media/ra Music/","label":"ra Music"},{"file":"addons://sources/audio/","label":"Music Add-ons"}]}}

When I use this code ...

String temp = //json code returned from up above
JSONObject obj = new JSONObject(temp);
JSONArray array = obj.getJSONArray("sources");

I get an error saying org.json.JSONObject Value... and then displays what is in temp. Any help?

3 Answers 3

2

The array named "sources" is several levels deep. You need to traverse down into the json.

Code formatters help with this stuff...

http://jsonformatter.curiousconcept.com/

{
   "id":1,
   "jsonrpc":"2.0",
   "result":{
      "limits":{
         "end":3,
         "start":0,
         "total":3
      },
      "sources":[
         {
            "file":"/media/storage/media/re Music/",
            "label":"re Music"
         },
         {
            "file":"/media/storage/media/ra Music/",
            "label":"ra Music"
         },
         {
            "file":"addons://sources/audio/",
            "label":"Music Add-ons"
         }
      ]
   }
}
Sign up to request clarification or add additional context in comments.

8 Comments

ok that makes more sense, but how would go into a nested statement like this? like this? JSONArray ar = obj.getaray("results")
@user987103 what is getarray? What is "results"?
I don't know the details of the exact library you are using, (you haven't actully told us though I probably could figure it out from the package name). Typically, you would do obj.get("result").getArray("sources"). Check your javadoc for the exact specifics on method namees
oh I'm just using org.json .. is there an easier one to use?
There are lots out there, but there are lots of stack overflow questions adressing the pros and cons. Which line gives you the error, the second or the third (in your original code)
|
0

It looks like the "sources" array is in the "result" object. So you would need to get that object and then get the array from that like this:

JSONObject obj = new JSONObject(temp);
JSONObject result = obj.getJSONObject("result");
JSONArray array = result.getJSONArray("sources");

1 Comment

everytime I put the result string into a JSONObject it throws that same exception
0

Your json should have top level object, from there you need to get child objects. See this link for more detail.

1 Comment

that's the thing though, once I put that temp string into a JSONObject it throws an error. I know that I have to go into a nested result, but it just throws that error.

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.