【问题标题】:Trouble with Parsing JSONObject in java在 java 中解析 JSONObject 的问题
【发布时间】:2013-08-19 23:31:57
【问题描述】:

我无法解析这个特定的 JSONObject,

这是对象:

{"status":1,"dds":{"id":1,"name":"DDS1","description":"This is DDS 1","children":[{"id":2,"order":1,"type":1,"children":[{"id":3,"order":1,"type":3,"children":[]},{"id":4,"order":2,"type":2,"children":[]}]}]}}

该对象存储在我的名为 result 的变量中,这是我解析它的代码:

JSONObject jsonObj = null;
JSONArray jsonArr = null;
try {
    jsonObj = new JSONObject(result);
    jsonArr = jsonObj.getJSONArray("dds");

} catch (JSONException e) { 
e.printStackTrace();
}

它给了我这个错误:

org.json.JSONException: Value {"id":1,"children":[{"type":1,"order":1,"id":2,"children":[{"type":3,"order":1,"id":3,"children":[]},{"type":2,"order":2,"id":4,"children":[]}]}],"description":"This is DDS 1","name":"DDS1"} at dds of type org.json.JSONObject cannot be converted to JSONArray

我正在尝试将其分解为子数组。我哪里错了?

@爱先生

这是我对您的代码的输出

【问题讨论】:

  • 我建议你使用 GSON 库进行 JSON 解析
  • 那会有什么不同?它会在安卓上运行吗?
  • @RiteshGune,我知道两者之间的区别。我现在才注意到服务器在这种情况下返回了一个对象,而我在所有其他情况下都返回了一个数组。但是我的问题现在是通过“孩子”将我的对象拆分为一个数组,但这给了我一个错误,我不明白为什么,我检查了拼写等。
  • Zapnologica,无意冒犯,只是想知道你是否知道。

标签: java json jsonobject


【解决方案1】:

你调用的是jsonArr = jsonObj.getJSONArray("dds");,但是dds不是一个数组,它是一个JSON对象,如果你格式化JSON你可以看得很清楚:

{
   "status":1,
   "dds":{
      "id":1,
      "name":"DDS1",
      "description":"This is DDS 1",
      "children":[
         {
            "id":2,
            "order":1,
            "type":1,
            "children":[
               {
                  "id":3,
                  "order":1,
                  "type":3,
                  "children":[

                  ]
               },
               {
                  "id":4,
                  "order":2,
                  "type":2,
                  "children":[

                  ]
               }
            ]
         }
      ]
   }
}

所以你只需要打电话给JSONObject dds = jsonObj.getJSONObject("dds"),如果你想要孩子,你可以打电话给JSONArray children = jsonObj.getJSONObject("dds").getJSONArray("children");

private static final String json = "{\"status\":1,\"dds\":{\"id\":1,\"name\":\"DDS1\",\"description\":\"This is DDS 1\",\"children\":[{\"id\":2,\"order\":1,\"type\":1,\"children\":[{\"id\":3,\"order\":1,\"type\":3,\"children\":[]},{\"id\":4,\"order\":2,\"type\":2,\"children\":[]}]}]}}";

public static void main(String[] args) throws JSONException
{
    JSONObject jsonObj = new JSONObject(json);
    JSONObject dds = jsonObj.getJSONObject("dds");

    JSONArray children = dds.getJSONArray("children");
    System.out.println("Children:");
    System.out.println(children.toString(4));

    JSONArray grandChildren = children.getJSONObject(0).getJSONArray("children");
    System.out.println("Grandchildren:");
    System.out.println(grandChildren.toString(4));
}

生产:

Children:
[{
    "children": [
        {
            "children": [],
            "id": 3,
            "order": 1,
            "type": 3
        },
        {
            "children": [],
            "id": 4,
            "order": 2,
            "type": 2
        }
    ],
    "id": 2,
    "order": 1,
    "type": 1
}]
Grandchildren:
[
    {
        "children": [],
        "id": 3,
        "order": 1,
        "type": 3
    },
    {
        "children": [],
        "id": 4,
        "order": 2,
        "type": 2
    }
]

【讨论】:

  • 好的,我将其更改为:JSONObject dds = (JSONObject) jsonObj.get("dds");,但现在JSONArray jsonArr = dds.getJSONArray("children"); 出现错误> 对孩子说没有价值
  • 如果你想通过某种方式得到这些孩子,他们确实是空的 {"type":2,"order":2,"id":4,"children":[ ]}]} 尝试将一些元素放入粗体数组中。
  • 好的,如果我运行它也可以,但我看到你在其中添加了 ``。所以我认为我的变量字符串结果可能有问题。
  • @MrLove ,为什么打印 children 和 grandchildren 时不显示 id: order: ?独生子女?
  • @Zapnologica 结果按字母顺序排列,它们位于子数组下方,但肯定存在。 (我在看到的输出中进行了编辑)。
【解决方案2】:

您可以这样做,其中 JsonElement 可以是 JSONobject 或 JsonArray 或任何原始类型:

private JsonElement findElementsChildren(JsonElement element, String id) {
    if(element.isJsonObject()) {
        JsonObject jsonObject = element.getAsJsonObject();
        if(id.equals(jsonObject.get("id").getAsString())) {
            return jsonObject.get("children");
        } else {
            return findElementsChildren(element.get("children").getAsJsonArray(), id);
        }
    } else if(element.isJsonArray()) {
        JsonArray jsonArray = element.getAsJsonArray();
        for (JsonElement childElement : jsonArray) {
            JsonElement result = findElementsChildren(childElement, id);
            if(result != null) {
                return result;
            }
        }
    }

    return null;
}

【讨论】:

  • 好的,现在它要求我在第一次返回时“添加演员表”,并用红色强调 .get
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-04
  • 2013-04-20
  • 2011-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多