java - 为什么这个 JSONObject 不是 JSONArray?

标签 java arrays org.json

我的javascript代码如下

var people = {
    "users" : [{id : this.getJID().toString()},{id : this.getJID().toString()}],
    "body" : messageBody
}

在我的服务器端,我有以下内容:

JSONObject b = new JSONObject(jsonstring);
JSONArray users = b.getJSONArray("users");

我无法获得用户,因为我会遇到以下异常。

org.json.JSONException: JSONObject["users"] is not a JSONArray.

我尝试转换为 JSONObject 但也出现错误。

JSONObject o = b.getJSONObject("users");

org.json.JSONException: JSONObject["users"] is not a JSONObject.

怎么了?

最佳答案

如上所述:

The JSON is serialized wrongly. The value of user is not an array but a string: {"users":"[...]","body":"test"} There should be no quotes around [...]. That's why you get the error. It seems like you recursively serialized the contents of people

关于java - 为什么这个 JSONObject 不是 JSONArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6952973/

相关文章:

java - 如何在Java类中获取当前数据库

asp.net-mvc - 将字符串数组参数传递给 MVC Controller

java - 如何在 Java 中克隆 org.json.JSONObject?

java - 想要获取枚举值而不是 JSON 中的名称

java - eclipse 调试 : Source not found for

c - 如何显示数组的内容并提示用户进行选择{C}

Javascript 动态数组和对象

android - Android 内置了哪个版本的 org.json?

java - 从 Json 文件中读取 JsonElement?

java - 在 Kotlin 的对象字段中具有上下文的 Android 类