Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to JSON array

Tags:

java

json

android

I have the following string of a JSON from a web service and am trying to convert this to a JSONarray

{     "locations": [         {             "lat": "23.053",             "long": "72.629",             "location": "ABC",             "address": "DEF",             "city": "Ahmedabad",             "state": "Gujrat",             "phonenumber": "1234567"         },         {             "lat": "23.053",             "long": "72.629",             "location": "ABC",             "address": "DEF",             "city": "Ahmedabad",             "state": "Gujrat",             "phonenumber": "1234567"         },         {             "lat": "23.053",             "long": "72.629",             "location": "ABC",             "address": "DEF",             "city": "Ahmedabad",             "state": "Gujrat",             "phonenumber": "1234567"         },         {             "lat": "23.053",             "long": "72.629",             "location": "ABC",             "address": "DEF",             "city": "Ahmedabad",             "state": "Gujrat",             "phonenumber": "1234567"         },         {             "lat": "23.053",             "long": "72.629",             "location": "ABC",             "address": "DEF",             "city": "Ahmedabad",             "state": "Gujrat",             "phonenumber": "1234567"         }     ] } 

I validated this String online, it seems to be correct. Now I am using the following code in android development to utilise

JSONArray jsonArray = new JSONArray(readlocationFeed); 

This throws exception a type mismatch Exception.

like image 412
Ashish Kasma Avatar asked Mar 25 '13 06:03

Ashish Kasma


1 Answers

Here you get JSONObject so change this line:

JSONArray jsonArray = new JSONArray(readlocationFeed);  

with following:

JSONObject jsnobject = new JSONObject(readlocationFeed); 

and after

JSONArray jsonArray = jsnobject.getJSONArray("locations"); for (int i = 0; i < jsonArray.length(); i++) {     JSONObject explrObject = jsonArray.getJSONObject(i); } 
like image 169
kyogs Avatar answered Oct 18 '22 05:10

kyogs