Hello friends i am having problem in parsing jSON web services data
I have the following set of data i got from my web services
[{"store_id":"81","store_name":"Mayo - Castlebar McDrive","store_type":"Drive-Thru",
"vouchers_available":"Vouchers available","store_limit":"10","distance":"8123.33 km",
"latitude":"53.8501090162671","longitude":"-9.29713726043701","image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/stores\/default.png",
"voucher_count":"2","is_open":"Restaurant Open","attributes":[{"attribute_name":"Wi-Fiiiii",
"image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/attributes\/t_wifi_icon.gif"},{"attribute_name":"Cashless",
"image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/attributes\/t_cashless_icon.gif"},
{"attribute_name":"McDrive","image_name":"http:\/\/www.mcfinder.ie\/admin\/images\/attributes\/car_icon.png"}]}]
and i m using this code to parse the DATA but i am getting the error
Please friends i am new in JSON Web services guide me what am i doing wrong.
CODE:
JSONObject jObject = new JSONObject(data);
JSONArray array = jObject.getJSONArray("attributes");
ERROR:
07-19 23:43:02.437: WARN/System.err(674): org.json.JSONException: A JSONObject text must begin with '{' at character 2 of
M waiting for some positive response and guidelines
Thanks
If you’re going to work with JSON data, I recommend reviewing the JSON introduction at http://json.org, until it is thoroughly understood. Luckily, JSON is a relatively simple data format, and becoming comfortable with it comes quickly.
To the specific problem in the original question, note that the outer-most structure of the JSON data is an array. So, it needs to be read as an array — not as an object.
Here’s a brief description of the complete JSON structure.
An array with one element that is an unnamed object. The unnamed object has twelve elements, eleven of which are strings, and one of which, named “attributes”, is an array of three objects. Each object in the “attributes” array has two string elements.
So, if you want the “attributes” array, first read in the entire contents as an array, then get the first component of the array as an object, then get the “attributes” element from that object as an array. Following is an example of doing this.
If you’re not stuck using the built-in JSON API that Android provides, I highly recommend switching to Jackson, which makes it very easy to read and write arbitrarily complex JSON with Java. Following is an example of using it.