anderjo4 anderjo4 - 2 years ago 111
Java Question

JSON parsing. Unexpected character (t) at position 2. JAVA

I'm trying to parse JSON data from a google maps search.
I've tryed both JACKSON and and now I'm Trying JSON SIMPLE. Both of them gives the same error.

First of all I'm doing an search on Google maps.

String urlString = ""+ "Stockholm" + "+Gym";

Gives me JSON while(1);{title:"stockholm Gym - Google Maps",url:"/maps?f=q\x26source=s_q\x26start=0\x26q=stockholm+Gym\x26ie=UTF8\x26hq=Gym.............. and so on.
I'm replacing the while(1); with ""; before i return the string.

To the problem when I'm trying to parse it

JSONParser parser = new JSONParser();

String jsonString = "";

// UriHandler.mapSearchJson is the method that returns the jsonString.

String jsonData = UriHandler.mapSearchJSON(jsonString);

Object obj = "";
try {

obj = parser.parse(jsonData);

} catch (ParseException e) {
// TODO Auto-generated catch block

JSONObject jsonObj = (JSONObject) obj;

String title = (String) jsonObj.get("title");

This gives me the exception.
Unexpected character (t) at position 2.

When I'm debbuging it. comes all the way to when it's trying to parse the string. then the obj is = null.

What in thw world am I doing wrong.


fvu fvu
Answer Source

As the others already mentioned, a nonquoted field name is not standard JSON. However, Jackson (and maybe others) has a set of option settings that allow it to work with nonstandard, but common JSON derivatives:


will enable processing of unquoted field names.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download