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
Sebastian Paaske Tørholm
51.3k11 gold badges103 silver badges123 bronze badges
2 Answers 2
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.
answered Jun 19, 2011 at 11:58
Don Roby
41.2k6 gold badges96 silver badges114 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
data appears to be an array of arrays. Perhaps you need to call ja.getJSONArray(i)?
answered Jun 19, 2011 at 11:41
Paul Tomblin
184k59 gold badges324 silver badges412 bronze badges
Comments
lang-java
[at the beginning and the]in the end? Like the error says,JSONObjectrequires the first character to be{!