0

我已经使用 GSON 将 Hashmap 转换为 json,json 字符串如下所示:

 {"records":[{"three":"chingi0","two":"Marge0","five":"Bart0","four":"Maggie0","one":"Homer0"},{"three":"chingi1","two":"Marge1","five":"Bart1","four":"Maggie1","one":"Homer1"}]}

但是在使用 json 解析这个字符串时,它给了我以下错误:

org.json.JSON.typeMismatch

以下是我的代码片段

JSONArray jArray=new JSONArray(str);//str is the json string
 JSONObject json_data;
            for(int i=0;i<jArray.length();i++){
                json_data = jArray.getJSONObject(i);
               //here i am accessing the data from json string

            }

我该如何解决这个问题?

这是我的堆栈跟踪:

org.json.JSONException: 值 {"records":[{"three":"chingi0","two":"Marge0","four":"Maggie0","five":"Bart0","one": "Homer0"},{"three":"chingi1","two":"Marge1","four":"Maggie1","five":"Bart1","one":"Homer1"}]} 类型org.json.JSONObject 无法转换为 JSONArray

4

1 回答 1

0

好吧,错误说明了一切,您实际上得到的是一个包含数组的对象,所以您应该做的是:

JSONObject obj=new JSONObject(str);

然后您将拥有该对象中包含的数组。

于 2012-10-10T13:08:50.063 回答