Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

java - Parse JSON Array

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...