简体   繁体   English

JSON解析。 位置2出现意外字符(t)。JAVA

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

I'm trying to parse JSON data from a google maps search. 我正在尝试从Google Maps搜索中解析JSON数据。 I've tryed both JACKSON and and now I'm Trying JSON SIMPLE. 我已经尝试了JACKSON,现在正在尝试JSON SIMPLE。 Both of them gives the same error. 他们两个都给出相同的错误。

First of all I'm doing an search on Google maps. 首先,我正在Google地图上进行搜索。

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. 给我JSON while(1); {title:“斯德哥尔摩体育馆-Google Maps”,url:“ / maps?f = q \\ x26source = s_q \\ x26start = 0 \\ x26q = stockholm + Gym \\ x26ie = UTF8 \\ x26hq = Gym ..............等等,在返回字符串之前,我将while(1);替换为“”;。

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. // UriHandler.mapSearchJson是返回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. 位置2出现意外字符(t)。

When I'm debbuging it. 当我调试时。 comes all the way to when it's trying to parse the string. 一直到尝试解析字符串时。 then the obj is = null. 那么obj为= null。

What in thw world am I doing wrong. 我到底在做什么错。

Thanks! 谢谢!

As the others already mentioned, a nonquoted field name is not standard JSON. 正如其他人已经提到的那样,未引用的字段名称不是标准的JSON。 However, Jackson (and maybe others) has a set of option settings that allow it to work with nonstandard, but common JSON derivatives: 但是,Jackson(也许还有其他人)具有一组选项设置 ,使其可以与非标准但通用的JSON派生一起使用:

JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES

will enable processing of unquoted field names. 将允许处理未加引号的字段名称。

响应不是有效的JSON,因为键名未用双引号引起来。

{title:"stockholm Gym" {title:“斯德哥尔摩体育馆”

is invalid JSON, it should be this: 无效的JSON,应该是这样的:

{"title":"stockholm Gym" 

Notice how title is surrounded by " double quotes 请注意title如何被"双引号"包围

You are pulling back Javascript code that is meant for the maps.google.com site to use. 您正在撤回供maps.google.com网站使用的Javascript 代码

There could be any Javascript code in that response, not just the JSON that happens to be returned as part of the search. 该响应中可能有任何Javascript代码,而不仅仅是在搜索过程中返回的JSON。

You need to request from their maps API instead: 您需要从他们的地图API请求:

http://maps.googleapis.com/maps/api/geocode/json?address=Stockholm+Gym&sensor=false http://maps.googleapis.com/maps/api/geocode/json?address=Stockholm+Gym&sensor=false

This will return you only the JSON data. 这将仅返回JSON数据。

Have a look the Google Maps API for more options. 看一下Google Maps API ,了解更多选项。

I faced this error when trying to parse the json returned from kafka (kafka twitter producer). 尝试解析从kafka(kafka Twitter生产者)返回的json时,我遇到了此错误。

The message returned was including some extra text other than json (KeyedMessage(twitter-test_english,null,null). Because of that I was facing this error. 返回的消息除了json(KeyedMessage(twitter-test_english,null,null))之外还包含一些其他文本。因此,我遇到了此错误。

KeyedMessage(twitter-test_english,null,null,{"created_at":"Sat Apr 23 18:31:10 +0000 2016","id":723942306777337856,"id_str":"723942306777337856"}

Pass only the message part from returned json and convert it into string. 仅传递返回的json中的消息部分,并将其转换为字符串。

{"created_at":"Sat Apr 23 18:31:10 +0000 2016","id":723942306777337856,"id_str":"723942306777337856"}

message = new KeyedMessage("twitter-test_english", (String)queue.take());
//System.out.println("This is message"+message.message());
String message_string = message.message().toString();
JsonParse.toParseJson(message_string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM