JSON得到没有对象的数组

时间:2016-01-18 17:28:38

标签: java android json

我正在尝试解析this JSON

{
  "listname": "red",
  "lists": [
    {
      "id": "01",
      "name": "paw",
      "list": [
        {
          "id": "A",
          "name": "pawa",
          "bar": "foo"
        },
        {
          "id": "B",
          "name": "pawb",
          "bar": "foo"
        }
      ]
    },
    {
      "id": "02",
      "name": "pew",
      "list": [
        {
          "id": "A",
          "name": "pewa",
          "bar": "foo"
        },
        {
          "id": "B",
          "name": "pewb",
          "bar": "foo"
        }
      ]
    },
    {
      "id": "03",
      "name": "piw",
      "list": [
        {
          "id": "A",
          "name": "piwa",
          "bar": "foo"
        },
        {
          "id": "B",
          "name": "piwb",
          "bar": "foo"
        }
      ]
    }
  ]
}

我把它放在Asset Folder上我读了它并将它转换为我String,因为这里一切都很好,问题是当我试图从name获取listslist的每个项目,并尝试从JSONObject obj = new JSONObject(str); JSONArray jsonMainArr = obj.getJSONArray("lists"); //first get the lists for (int i = 0; i < jsonMainArr.length(); i++) { JSONObject childJSONObject = jsonMainArr.getJSONObject(i); String name = childJSONObject.getString("name"); Toast.makeText(context, name, Toast.LENGTH_SHORT).show(); } JSONObject obj = new JSONObject(str); JSONArray jsonMainArr = obj.getJSONArray("list"); //get the list for (int i = 0; i < jsonMainArr.length(); i++) { JSONObject childJSONObject = jsonMainArr.getJSONObject(i); String name = childJSONObject.getString("name"); Toast.makeText(context, name, Toast.LENGTH_SHORT).show(); } 获取所有名称我尝试过这样做:

JSON

但它没有显示任何东西......我缺少什么?

修改

这就是我阅读 public static String loadJSONFromAsset(Context ctx, String str) { String json = null; try { InputStream is = ctx.getAssets().open(str); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); json = new String(buffer, "UTF-8"); } catch (IOException ex) { ex.printStackTrace(); return null; } return json; }

的方式
import blaze as bz

bdata = bz.Data([(1, 'Alice', 100.9, 100),
           (2, 'Bob', 200.6, 200),
           (3, 'Charlie', 300.45, 300),
           (5, 'Edith', 400, 400)],
          fields=['id', 'name', 'revenue', 'profit'])

1 个答案:

答案 0 :(得分:2)

因为你的&#34;列表&#34;嵌套在'\w'(来自&#34;列表&#34;)中,嵌套for循环以检索这组值(JSONobject)

childJSONObject