0

我需要用 java 中的简单 JSON 读取数组列表。我得到了代码,但是我应该写什么让它读取树“example-array”,它是主树“Example”的子树。

这是json代码:

{
  "Example": {
    "example-array": [
      "something"
}

我尝试的是阅读它

JSONArray example = (JSONArray) jsonObject.get("Example.example-array");

但这不起作用。请帮忙。

4

1 回答 1

1

第一件事:你的 JSON 格式不正确,你没有用“]”关闭数组。

但除此之外,试试这个:

JSONParser parser=new JSONParser();

System.out.println("=======decode=======");

String s="{\"Example\":{\"example-array\":[\"something\"]}}";

Object obj=parser.parse(s);
JSONObject jObj=(JSONObject)obj;
JSONObject jObj2=(JSONObject)jObj.get("Example");
JSONArray jArr = (JSONArray)jObj2.get("example-array");

System.out.println(jArr);
System.out.println(jArr.get(0));
于 2015-03-23T20:51:12.273 回答