简体   繁体   English

Json String无法使用json-simple toolkit java进行解码

[英]Json String can't decode using json-simple toolkit java

JSON-Simple JSON-简单

JSON-Simple Example JSON-简单示例

String login = "{\"result\":[104192,42068],\"id\":1}";

Object obj = JSONValue.parse(login);

JSONArray array = (JSONArray)obj;

This throw a exception 抛出异常

Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray 线程“main”中的异常java.lang.ClassCastException:org.json.simple.JSONObject无法强制转换为org.json.simple.JSONArray

What is the problem in this code ? 这段代码有什么问题?

在这种情况下,解析后的结果是JSONObject因此您需要将其JSONObjectJSONObject

I had this problem and I have fixed it. 我有这个问题,我已经解决了。 Here is my code snippet: 这是我的代码片段:

Object obj = JSONValue.parse(response.getBody());
JSONObject jsonObject = (JSONObject) obj;
Object gu_obj = jsonObject.get("guid");

I got the yahoo user guid from the request body. 我从请求正文中获得了yahoo用户guid。 Hope this helps :) 希望这可以帮助 :)

You are trying to cast an object into an array. 您正在尝试将对象转换为数组。 Try adding the object to the array instead. 尝试将对象添加到数组中。

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

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