0

the code which i used its working perfectly on localhost but i tried to excute that on my web server not getting whats wrong in parsing its showing wrror while parsing from string to JSON object

My JSON Parser Code is

try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "utf-8"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();
    json = sb.toString();
    Log.d("converted result", json);
} catch (Exception e) {
    Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try parse the string to a JSON object
try {
    jObj = new JSONObject("{" + json +"}");
} catch (JSONException e) {
    Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
return jObj;

LogCat Information

01-29 11:45:53.556: D/Search keyword:(921): saint louis
01-29 11:45:57.288: D/converted result(921): 
connected{"products":[{"pid":"406355","name":"852610 SOUTHCOUNTYMALL"},{"pid":"406356","name":"852611 SOUTHCOUNTYMALL"},{"pid":"406357","name":"852612 SOUTHCOUNTYMALL"}],"success":1}

01-29 11:45:57.288: E/JSON Parser(921): Error parsing data org.json.JSONException: Expected ':' after connected at character 11 of {connected{"products":[{"pid":"406355","name":"852610 SOUTHCOUNTYMALL"},{"pid":"406356","name":"852611 SOUTHCOUNTYMALL"},{"pid":"406357","name":"852612 SOUTHCOUNTYMALL"}],"success":1}
01-29 11:45:57.288: E/JSON Parser(921): }
7
  • if you remove the connected word, what remains is a valid Json object Commented Jan 29, 2013 at 17:05
  • 2
    Where is the "connected" piece from the beginning of the string coming from. That's your invalid json... Commented Jan 29, 2013 at 17:07
  • @Pete i am new to JSON please help me how to remove connected Commented Jan 29, 2013 at 17:14
  • @Devender, it's coming from your stream, so it's somewhere before the code you've got posted, so I have no idea. Commented Jan 29, 2013 at 17:17
  • @Pete is that possible to Trim that in JSON parser Commented Jan 29, 2013 at 17:25

2 Answers 2

4

As the error clearly states, your JSON is not valid, because of the word connected in front of it.

You need to make your server stop sending that word.

Sign up to request clarification or add additional context in comments.

Comments

1

You have this JSON:

{connected{"products":[{"pid":"406355","name":"852610 SOUTHCOUNTYMALL"},{"pid":"406356","name":"852611 SOUTHCOUNTYMALL"},{"pid":"406357","name":"852612 SOUTHCOUNTYMALL"}],"success":1}

This is an invalid JSON format because you have the wrong expresion '{connected' in the beginning of the JSON. Maybe, you should get a JSON like this:

{"products":[{"pid":"406355","name":"852610 SOUTHCOUNTYMALL"},{"pid":"406356","name":"852611 SOUTHCOUNTYMALL"},{"pid":"406357","name":"852612 SOUTHCOUNTYMALL"}],"success":1}

or like this:

{"connected":[VALUE], "products":[{"pid":"406355","name":"852610 SOUTHCOUNTYMALL"},{"pid":"406356","name":"852611 SOUTHCOUNTYMALL"},{"pid":"406357","name":"852612 SOUTHCOUNTYMALL"}],"success":1}

Anyway, I recommend you to use the Gson library to build and get JSONs.

2 Comments

You also can use this web to validate your JSON's syntax: jsonformatter.curiousconcept.com
or like: {"connected":{"products":[{"pid":"406355","name":"852610 SOUTHCOUNTYMALL"},{"pid":"406356","name":"852611 SOUTHCOUNTYMALL"},{"pid":"406357","name":"852612 SOUTHCOUNTYMALL"}],"success":1}}

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.