0

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

3 Answers 3

1

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.

import java.math.BigDecimal;
import java.net.URI;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    JSONArray outerArray = new JSONArray("[{\"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\"}]}]");
    JSONObject object = outerArray.getJSONObject(0);
    JSONArray attributes = object.getJSONArray("attributes");
    for (int i = 0, length = attributes.length(); i < length; i++)
    {
      JSONObject attribute = attributes.getJSONObject(i);
      System.out.printf("attribute name=%s, image=%s\n", attribute.getString("attribute_name"), attribute.getString("image_name"));
    }
  }
}

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.

import java.io.File;
import java.math.BigDecimal;
import java.net.URI;
import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper();
    Store[] stores = mapper.readValue(new File("input.json"), Store[].class);
    Store store = stores[0];
    List<Attribute> attributes = store.attributes;
    for (Attribute attribute : attributes)
    {
      System.out.printf("attribute name=%s, image=%s\n", attribute.attribute_name, attribute.image_name);
    }
    // output:
    // attribute name=Wi-Fiiiii, image=http://www.mcfinder.ie/admin/images/attributes/t_wifi_icon.gif
    // attribute name=Cashless, image=http://www.mcfinder.ie/admin/images/attributes/t_cashless_icon.gif
    // attribute name=McDrive, image=http://www.mcfinder.ie/admin/images/attributes/car_icon.png
  }
}

class Store
{
  public String store_id;
  public String store_name;
  public String store_type;
  public String vouchers_available;
  public String store_limit;
  public String distance;
  public BigDecimal latitude;
  public BigDecimal longitude;
  public URI image_name;
  public int voucher_count;
  public String is_open;
  public List<Attribute> attributes;
}

class Attribute
{
  public String attribute_name;
  public URI image_name;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanx im sure ur Right.. please can u guide me a little how can i pass the String to JSONArray object. since it takes JSONObject as a parameter in its constructor.
Thanks alot Programmer Bruce It worked for me... I think you are right i need to work alot on JSON before implementing it. Thanks again
1

The answer by @Richard aka cyberkiwi for this question should be the answer for your question.

He says:

You may be passing the STRING to JSONObject with leading spaces. Try trimming

JSONObject allCDs = new JSONObject(objectString.replace(/^\s+/,""));

EDIT: I thought this was javascript. Try trimming it using Java code instead

JSONObject allCDs = new JSONObject(objectString.trim());

If that still doesn't work, then show what the first character from the string is:

System.out.println((int)objectString.trim().charAt(0));

You should be expecting 123, the curly braces. In fact, check the entire content

System.out.println((int)objectString);  // or
System.out.println((int)objectString.trim());

You could also try cutting everything before the { in the string

JSONObject allCDs = new JSONObject(objectString.substring(objectString.indexOf('{')));

1 Comment

no its not actually what i have noticed the First character of the Json Data should be "{" but it has "[" as a first character. that i why its giving error. Please guide me what can i do with that..
0

If you're taking the Udacity android course and encountering this error for the quakereport/DidUfeelIt app then change the URL and try with some other URL your problem will be solved. Eg:- The URL provided by during the course was "http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2012-01-01&endtime=2012-12-01&minmagnitude=6"

Then I was getting the same error that is "problem parsing the JSON" So I tried different URL: https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson

And it worked..!! Always try to get the latest URL's from the USGS website during the course.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.