anderjo4 anderjo4 - 7 months ago 5
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 = "http://maps.google.com/maps?f=q&source=s_q&output=json&start=0&q="+ "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
e.printStackTrace();
}

JSONObject jsonObj = (JSONObject) obj;

String title = (String) jsonObj.get("title");
System.out.println(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.

Thanks!

fvu fvu
Answer

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:

JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES

will enable processing of unquoted field names.

Comments