0

From this page,

org.json.JSONException: Expected literal value at character 550 of

From my asp.net webservice I GET

   "[{\"ID\":1,\"Title\":\"When Harry Met Sally\",\"ReleaseDate\":\"1989-01-11T00:00:00\",\"Genre\":\"Romantic Comedy\",\"Price\":7.99,\"Rating\":null},{\"ID\":2,\"Title\":\"Ghostbusters \",\"ReleaseDate\":\"1984-03-13T00:00:00\",\"Genre\":\"Comedy\",\"Price\":8.99,\"Rating\":null},{\"ID\":3,\"Title\":\"Ghostbusters 2\",\"ReleaseDate\":\"1986-02-23T00:00:00\",\"Genre\":\"Comedy\",\"Price\":9.99,\"Rating\":null},{\"ID\":4,\"Title\":\"Rio Bravo\",\"ReleaseDate\":\"1959-04-15T00:00:00\",\"Genre\":\"Western\",\"Price\":3.99,\"Rating\":null},{\"ID\":5,\"Title\":\"Gone With The wind\",\"ReleaseDate\":\"1981-01-01T00:00:00\",\"Genre\":\"Hippys\",\"Price\":10.00,\"Rating\":\"Ok\"},{\"ID\":6,\"Title\":\"Happy Hour\",\"ReleaseDate\":\"1911-01-01T00:00:00\",\"Genre\":\"Supers\",\"Price\":33.00,\"Rating\":\"Good\"},{\"ID\":7,\"Title\":\"Happy Hour\",\"ReleaseDate\":\"1911-01-01T00:00:00\",\"Genre\":\"Supers\",\"Price\":33.00,\"Rating\":\"Good\"}]"

I receive it:

    "[{\"ID\":1,\"Title\":\"When Harry Met Sally\",\"ReleaseDate\":\"1989-01-11T00:00:00\",\"Genre\":\"Romantic Comedy\",\"Price\":7.99,\"Rating\":null},{\"ID\":2,\"Title\":\"Ghostbusters \",\"ReleaseDate\":\"1984-03-13T00:00:00\",\"Genre\":\"Comedy\",\"Price\":8.99,\"Rating\":null},{\"ID\":3,\"Title\":\"Ghostbusters 2\",\"ReleaseDate\":\"1986-02-23T00:00:00\",\"Genre\":\"Comedy\",\"Price\":9.99,\"Rating\":null},{\"ID\":4,\"Title\":\"Rio Bravo\",\"ReleaseDate\":\"1959-04-15T00:00:00\",\"Genre\":\"Western\",\"Price\":3.99,\"Rating\":null},{\"ID\":5,\"Title\":\"Gone With the Wind\",\"ReleaseDate\":\"1990-01-01T00:00:00\",\"Genre\":\"hippys\",\"Price\":40.00,\"Rating\":\"ok\"},{\"ID\":6,\"Title\":\"Scary\",\"ReleaseDate\":\"1990-01-01T00:00:00\",\"Genre\":\"Hippys\",\"Price\":30.00,\"Rating\":\"Ok\"},{\"ID\":7,\"Title\":\"Fox Trot\",\"ReleaseDate\":\"1960-04-04T00:00:00\",\"Genre\":\"Gold\",\"Price\":20.00,\"Rating\":\"Magic\"},{\"ID\":8,\"Title\":\"Happy Hour\",\"ReleaseDate\":\"1911-01-01T00:00:00\",\"Genre\":\"Supers\",\"Price\":33.00,\"Rating\":\"Good\"}]"

I then use the code below, found on the page above, to edit the string to remove the slashes, the inverted commas, and the n, and then finally I can use it. I am sure this is the worst possible way. I am also sure that it must be the way my web service is sending the data. What is a more pleasing way of doing this?

The result is:

   "[{"ID":1,"Title":"When Harry Met Sally","ReleaseDate":"1989-01-11T00:00:00","Genre":"Romantic Comedy","Price":7.99,"Rating":null},{"ID":2,"Title":"Ghostbusters ","ReleaseDate":"1984-03-13T00:00:00","Genre":"Comedy","Price":8.99,"Rating":null},{"ID":3,"Title":"Ghostbusters 2","ReleaseDate":"1986-02-23T00:00:00","Genre":"Comedy","Price":9.99,"Rating":null},{"ID":4,"Title":"Rio Bravo","ReleaseDate":"1959-04-15T00:00:00","Genre":"Western","Price":3.99,"Rating":null},{"ID":5,"Title":"Gone With the Wind","ReleaseDate":"1990-01-01T00:00:00","Genre":"hippys","Price":40.00,"Rating":"ok"},{"ID":6,"Title":"Scary","ReleaseDate":"1990-01-01T00:00:00","Genre":"Hippys","Price":30.00,"Rating":"Ok"},{"ID":7,"Title":"Fox Trot","ReleaseDate":"1960-04-04T00:00:00","Genre":"Gold","Price":20.00,"Rating":"Magic"},{"ID":8,"Title":"Happy Hour","ReleaseDate":"1911-01-01T00:00:00","Genre":"Supers","Price":33.00,"Rating":"Good"}]"

Real JSON.

Below are the code snippets I am using.

My Web Service in C#

    private MovieDBContext db = new MovieDBContext();

    // Localhost/api/MovieApi/
    // GET api/<controller>

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string Get()
    {

        var GenreList = new List<string>();

        var GenQry = from d in db.Movies
                     orderby d.Genre
                     select d.Genre;

        GenreList.AddRange(GenQry.Distinct());


        var movies = from m in db.Movies
                     select m;

        string json = JsonConvert.SerializeObject(movies);

        return json;

    }

and my Android that turns this to usable json, and into a JSON array

    public String convertStandardJSONString(String data_json){
    data_json = data_json.replace("\\", "");
    data_json = data_json.replace("\"{", "{");
    data_json = data_json.replace("}\",", "},");
    data_json = data_json.replace("}\"", "}");
    return data_json;
}

public List<Movie> findAllItems(String movies) {

    movies = convertStandardJSONString(movies);

    movies = movies.substring(1, movies.length()-1);



    JSONArray jArray = null;
    try {
        jArray = new JSONArray(movies);
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

...

And the Async Task

    public String requestWebService(String serviceUrl) {
    // Making HTTP request        
   try {
      // defaultHttpClient
      DefaultHttpClient httpClient = new DefaultHttpClient();
      //HttpPost httpPost = new HttpPost(serviceUrl);
      HttpGet httpGet = new HttpGet(serviceUrl);
      HttpResponse httpResponse = httpClient.execute(httpGet);//);Post);
      HttpEntity httpEntity = httpResponse.getEntity();
      is = httpEntity.getContent();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    try {
      BufferedReader reader = new BufferedReader(new InputStreamReader(
          is, "iso-8859-1"), 8);
      StringBuilder sb = new StringBuilder();
      String line = null;
      while ((line = reader.readLine()) != null) {
        sb.append(line + "n");
      }
      is.close();
      json = sb.toString();
    } catch (Exception e) {
      Log.e("Buffer Error", "Error converting result " + e.toString());
    }
    // try parse the string to a JSON object

    // return JSON String
    return json;
  }
Community
  • 1
  • 1
iSimply
  • 49
  • 9

1 Answers1

1

I think this line looks a little bit suspicious:

while ((line = reader.readLine()) != null) {
   sb.append(line + "n");
}

Hope that helps you with debugging.

iluu
  • 406
  • 3
  • 13