Reputation: 21
My JSON is something like this:
[{
"myviews":[{
"2011-05-12_2011-05-14":{
"name":"thiswk",
"data":[[12,
2403
],
[13,
2082
],
[14,
5823
]
]
}
},
{
"2011-06-05_2011-06-7":{
"name":"lastwk",
"data":[[5,
1279
],
[6,
6685
],
[7,
2163
]
]
}
}
]
}
]
JSONObject jo = new JSONObject(jsonString);
JSONArray ja;
jo = jo.getJSONObject("2011-05-12_2011-05-14");
ja = jo.getJSONArray("data");
int resultCount = ja.length();
for (int i = 0; i < resultCount; i++)
{
JSONObject resultObject = ja.getJSONObject(i);
resultObject.getJSONArray("12");
System.out.println("--");
}
I am unable to read the values under the "data" array. Get this error
Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at character 1
Upvotes: 1
Views: 24982
Reputation: 41137
You're trying to create a JSONObject based on a string that doesn't represent an object, but an array containing one object.
To get the contained object, try
JSONArray inputArray = new JSONArray(jsonString);
JSONObject jo = inputArray.getJSONObject(0);
I think some of your later work is wrong as well, but perhaps this will get you started.
Upvotes: 8
Reputation: 182782
data
appears to be an array of arrays. Perhaps you need to call ja.getJSONArray(i)
?
Upvotes: 0