JSON typeMismatch

时间:2014-06-18 09:49:50

标签: android json arrays

我试图从JSON解析数据但是我收到一个错误,说JSONType不匹配,

我用来解析数据的代码如下

    arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions.getJSONfromURL(urlString);

            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("product");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    map.put("name", jsonobject.getString("name"));
                    map.put(AppConstants.SUB_CATEGORY_HREF, jsonobject.getString(AppConstants.SUB_CATEGORY_HREF))   ;
                    // Set the JSON Objects into the array
                    arraylist.add(map);

                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }

json数组结构如下

              {
"success": true,
"product": {
    "id": "42",
    "name": "product name",
    "description": "<p>Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here.</p>  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> ",
    "image": "http://test.com/image/cache/data/products/eyes-600x600.png",
    "image1": "http://test.com/image/cache/data/products/product2-600x600.png",
    "image2": "http://test.com/image/cache/data/products/lips-600x600.png",
    "image3": "http://test.com/image/cache/data/products/product6-600x600.png",
    "image4": null,
    "price": "100KD",
    "special": "70KD",
    "related products": [
        {
            "product_id": "30",
            "thumb": "http://image/cache/data/products/product6-600x600.png",

        },
        {
            "product_id": "35",
            "thumb": "http://image/cache/data/products/eyes-600x600.png",

        }
    ]
}
}

详细的logcat输出正在给出

06-18 15:17:02.834: W/System.err(8183):     atorg.json.JSON.typeMismatch(JSON.java:100)
06-18 15:17:02.834: W/System.err(8183):     at org.json.JSONObject.getJSONArray(JSONObject.java:553)
06-18 15:17:02.834: W/System.err(8183):     at com.dairam.fragments.ProductDetailsFragment$DownloadJSON.doInBackground(ProductDetailsFragment.java:82)
06-18 15:17:02.834: W/System.err(8183):     at com.dairam.fragments.ProductDetailsFragment$DownloadJSON.doInBackground(ProductDetailsFragment.java:1)
06-18 15:17:02.834: W/System.err(8183):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-18 15:17:02.844: W/System.err(8183):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-18 15:17:02.844: W/System.err(8183):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-18 15:17:02.854: W/System.err(8183):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-18 15:17:02.854: W/System.err(8183):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-18 15:17:02.854: W/System.err(8183):     at java.lang.Thread.run(Thread.java:841)

3 个答案:

答案 0 :(得分:2)

正如其他人所指出的那样:

jsonarray = jsonobject.getJSONArray("product");

正在尝试检索一个名为product的数组,该数组不存在。产品是一个对象。 您可以通过执行以下操作来缓解错误:

jsonObject = jsonobject.getJSONObject("product");

这假定您已更改变量的类型。但是,您可以使用jsonObject.get();

访问对象的其他部分

答案 1 :(得分:1)

使用以下代码行查找json数组

jsonarray = jsonobject.getJSONArray("product");
下面的

是json的json对象。所以错误是类型错配。

{
  "success": true,
  "product": {
"id": "42",
"name": "product name",
"description": "<p>Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here.</p>  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here. Your product description will be here.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> ",
"image": "http://test.com/image/cache/data/products/eyes-600x600.png",
"image1": "http://test.com/image/cache/data/products/product2-600x600.png",
"image2": "http://test.com/image/cache/data/products/lips-600x600.png",
"image3": "http://test.com/image/cache/data/products/product6-600x600.png",
"image4": null,
"price": "100KD",
"special": "70KD",
"related products": [
    {
        "product_id": "30",
        "thumb": "http://image/cache/data/products/product6-600x600.png",

    },
    {
        "product_id": "35",
        "thumb": "http://image/cache/data/products/eyes-600x600.png",

    }
]
}
}

json数组以[开头,以]

结尾

例如related products是json数组

"related products": [
    {
        "product_id": "30",
        "thumb": "http://image/cache/data/products/product6-600x600.png",

    },
    {
        "product_id": "35",
        "thumb": "http://image/cache/data/products/eyes-600x600.png",

    }
]

答案 2 :(得分:0)

基本上,当您获得数组或对象类型的json数据时,会产生此问题。如果它不匹配,您将收到此类错误。请使用:

  • 对于jsonarray数据

    jsonarray = jsonobject.getJSONArray("product");
    
  • 对于jsonObject数据

    jsonarray = jsonobject.getJSONObject("product");