简体   繁体   English

在 java 中出现错误 JSONObject 无法转换为 JSONArray?

[英]Getting a error JSONObject cannot be cast to JSONArray in java?

i need some help to fix this issue, I am having: java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray .我需要一些帮助来解决这个问题,我有: java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray I tried by wrapping [] over my jsonobject but that did not work.我尝试将[]包裹在我的 jsonobject 上,但这不起作用。 can anyone help me solve this issue with my code below.谁能帮我用下面的代码解决这个问题。 thank you!谢谢你!

here is my code:这是我的代码:

 JSONParser parser = new JSONParser();
jsonArray = (JSONArray) parser.parse(response);


JSONObject jsonObject;
jsonObject = (JSONObject) jsonArray.get(0);
String email = jsonObject.get("EmailAddress").toString();

The standard JSON library from json.org for java is designed for dynamic languages and as a consequence absolutely sucks on java. java 的标准 JSON 库来自json.org ,专为动态语言而设计,因此绝对不适合 Z923F425A48B323 Don't use it;不要使用它; get something like Jackson or GSON instead.改用 Jackson 或 GSON 之类的东西。

The problem is that whatever JSON you have isn't an array, it's for example {data: [{a: 1}, {b: 2}]} , which is sensible, as JSON technically implies that the 'top-level' thing MUST be an object or it isn't considered valid, even though many, many, many usages and implementations flaunt that rule (JSON has the rather significant downside of everyone piling on addendums and caveats on top of a simple standard, unfortunately. Rolling with those punches is part of the job).问题是,无论您拥有什么 JSON 都不是数组,例如{data: [{a: 1}, {b: 2}]} ,这是明智的,因为 JSON 在技术上暗示“顶级”东西必须是 object 或者它被认为是无效的,即使许多、许多、许多用法和实现都标榜该规则(不幸的是,JSON 有一个相当大的缺点,即每个人都在一个简单的标准之上堆积附录和警告。滚动用这些拳头是工作的一部分)。

In other words, parser.parse(response) is returning a JSONObject because you're parsing a, well, JSON object: a thing in {} , not in [] .换句话说, parser.parse(response)正在返回一个JSONObject ,因为您正在解析一个 JSON object: a thing in {} ,而不是[] Given that you haven't pasted your actual JSON, Who knows what that means.鉴于您尚未粘贴实际的 JSON,谁知道这意味着什么。 Possibly there's only one element being sent to you, eg you want:可能只有一个元素被发送给您,例如您想要:

// JSON looks like:
// {"EmailAddress": "something@somewhere"}
JSONParser parser = new JSONParser();

JSONObject jsonObject = (JSONObject) parser.parse(response);
String email = jsonObject.get("EmailAddress").toString();

Or possibly the output array is itself wrapped in a JSON object with a single value, eg you want:或者可能 output 数组本身包装在 JSON object 中,具有单个值,例如您想要:

// JSON looks like:
// {"data": [{"EmailAddress": "something@somewhere"}]}

JSONParser parser = new JSONParser();
JSONObject main = (JSONObject) parser.parse(response);
jsonArray = main.getJSONArray("data");

JSONObject jsonObject;
jsonObject = (JSONObject) jsonArray.get(0);
String email = jsonObject.get("EmailAddress").toString();

Actually, you don't want either of those, you want to delete that and read a tutorial on Jackson or GSON instead.实际上,您不想要其中任何一个,您想删除它并阅读有关 Jackson 或 GSON 的教程。

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

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