1

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
asked Jun 19, 2011 at 11:35
2
  • 2
    Did you try removing the [ at the beginning and the ] in the end? Like the error says, JSONObject requires the first character to be {! Commented Jun 19, 2011 at 11:37
  • 4
    Why do you not believe the error message? Commented Jun 19, 2011 at 11:38

2 Answers 2

8

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
Sign up to request clarification or add additional context in comments.

Comments

0

data appears to be an array of arrays. Perhaps you need to call ja.getJSONArray(i)?

answered Jun 19, 2011 at 11:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.