Reputation: 1068
am parsing a json string. When i run my code am receiving json exception which says "type mismatch" at a point where am getting json array from json object. here is my code
String dataFromLogin="{"catego":{"id":"2","fname":"Tashen Jazbi","uname":"tashen",
"password":"123","pic_url":"","lati":"33.7167","longi":"73.0667","city":"Islamabad",
"country":"Pakistan","mobid":"000000000000000","street":"xyz",
"dateandtime":"2013-12-29 18:07:52"}}";
try {
JSONObject jsonObj = new JSONObject(dataFromLogin);
//JSONObject response = jsonObj.getJSONObject("catego");
JSONArray contacts = jsonObj.getJSONArray("catego");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
fullname = c.getString("fname");
uname = c.getString("uname");
pic_url = c.getString("pic_url");
lat = c.getString("lati");
lng = c.getString("longi");
city = c.getString("city");
country = c.getString("country");
street= c.getString("street");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I couldn't found where am doing wrong. If anyone can help then it'll be much appreciated.
Thanks :)
Upvotes: 0
Views: 1801
Reputation: 133560
JSONArray contacts = jsonObj.getJSONArray("catego");
catego
is a jsonobject
{
represents a json object node
[
represents a json array node
Upvotes: 7
Reputation: 2879
Use this:
JSONObject response = jsonObj.getJSONObject("catego");
because "catego" is an object not an array.
Upvotes: 1