I'm having trouble parsing some JSON in a java application.

The JSON format is as follows:

{"values": [["kevin", "a value"], ["another name", "another value"]], "cols":["name", "val"]}

My code to parse it this ("results" contains the raw JSON string):

JSONObject myobj = new JSONObject(results);
JSONArray json = myobj.getJSONArray("values");

for(int i = 0; i < json.length(); i++){

    JSONArray tmpArr = json.getJSONObject(i).getJSONArray("values");

    for(int j = 0; j < tmpArr.length(); j++){
        System.out.println(tmpArr.getJSONObject(j).toString());
    }
}

This is giving me a JSON typeMismatch error.

My end goal is to be able to use this JSON method that I'm used to working with to get individual values out of my data:

jsonObject.getString("name");

Where name would be the column/attribute name specified in the "cols" in the JSON string.

Any help is very much appreciated.

share|improve this question
1  
There is no element named "Value". – Hot Licks Jan 16 at 1:45
@HotLicks the "Value" was a typo sorry, it should've been "values". So there's no way to deal with this? I was under the impression that it's some sort of dictionary. – StarFan1990 Jan 16 at 1:49
Why are you getting "values" twice. The second one is going to go wanting, I think. – Hot Licks Jan 16 at 1:49
You've got an object with two entries -- "values" and "cols". The "values" and "cols" entries are, in turn, arrays. "values" is an array containing two arrays. "cols" contains just two strings. – Hot Licks Jan 16 at 1:52
@HotLicks So if I just do JSONArray json = myObj.getJSONArray("Values"), "json" should now contain the array of ["kevin", "a value"], ["another name", "another value"]. Is there a way to map these nested arrays into objects by using jsonObject.getString("name") since the data mappings are declared in the "cols" object? – StarFan1990 Jan 16 at 1:52
show 5 more comments
feedback

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.