achu achu - 3 months ago 9
Android Question

JSON typeMismatch

Am trying to parse the data from JSON the But I am getting an error saying JSONType mismatch,

The code I am using to parse the data is as follow

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();
}


the json array structure is as follows

{
"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",

}
]
}
}


The detailed logcat output is giving

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)

Answer

As others have pointed out, the line:

jsonarray = jsonobject.getJSONArray("product");

Is trying to retrieve an array called product, which doesn't exist. product is an object. You can mitigate the error, by doing:

jsonObject = jsonobject.getJSONObject("product");

This assumes that you have changed the type of your variable. But then you can access other parts of the object with jsonObject.get();