0

我在解析我的 JSON 文件时遇到错误

输入

{"大陆":"南美洲","re​​centJobRank":717,"latitude":"-34.6037232","lastSeenDate":"2012-11-23","start":"Inmediato","contactPerson":" Alejandra Perez","lastJobRank":2,"title":"Encimador","salary":"Convenio","jobtype":"Tiempo Completo","url":"http://www.computrabajo.com. ar/bt-ofrd-deglay-7148.htm","postedDate":"2012-11-21","duration":"Indeterminada","firstSeenDate":"2012-11-23","phoneNumber":" 011 4648-0226 RRHH","faxNumber":"011 4648-0226","location":"阿根廷布宜诺斯艾利斯","company":"Deglay SRL","id":"34076","department":"Buenos Aires","category":"others","applications":"Por e-mail o comunicandose a los telefonos","longitude":"-58.3815931"}

以下是我收到的例外情况

例外

位置 457 处的意外字符 (J)。在 org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269) 的 org.json.simple.parser.Yylex.yylex(Yylex.java:610) 的 addfields 中捕获异常)

我试过在验证器上检查我的 json。看起来很好。我犯了什么明显的错误?

4

1 回答 1

0

JSON 绝对正确,因为 JSON.parse() 接受它。我无法使用 json-simple 库真正重现您的错误。我在这里下载了每个可用的版本,复制粘贴了您的 JSON 字符串并将其传递给JSONParser.parse()并且在任何版本中都没有错误。

这是我的设置:

public static void main(String[] args)  {
    try {
      StringReader x = new StringReader("{\"continent\":\"South America\",\"recentJobRank\":717,\"latitude\":\"-34.6037232\",\"lastSeenDate\":\"2012-11-23\",\"start\":\"Inmediato\",\"contactPerson\":\"Alejandra Perez\",\"lastJobRank\":2,\"title\":\"Encimador\",\"salary\":\"Convenio\",\"jobtype\":\"Tiempo Completo\",\"url\":\"http://www.computrabajo.com.ar/bt-ofrd-deglay-7148.htm\",\"postedDate\":\"2012-11-21\",\"duration\":\"Indeterminada\",\"firstSeenDate\":\"2012-11-23\",\"phoneNumber\":\"011 4648-0226 RRHH\",\"faxNumber\":\"011 4648-0226\",\"location\":\"Buenos Aires, Argentina\",\"company\":\"Deglay S.R.L.\",\"id\":\"34076\",\"department\":\"Buenos Aires\",\"category\":\"others\",\"applications\":\"Por e-mail o comunicandose a los telefonos\",\"longitude\":\"-58.3815931\"}");
      new JSONParser().parse(x);
    } catch(Exception e) {
       System.out.println("Error: " + e);
    }

    System.out.println("Success");    
}

所以我认为你的 JSON 和库都没有问题。我的猜测是您的 JSON 字符串的编码,因为错误消息说

位置 457 处的意外字符 (J)。

并且在这个位置附近没有任何J。因此,您收到的 JSON 要么以某种方式编码,SimpleJSON 无法正确解析,要么数据没有完全/正确地传输。

也许它可以帮助你知道你从哪里得到 JSON 以及你如何将它传递给 JSONParser.parse()。

于 2014-11-08T11:31:24.013 回答