将整个JSON对象添加到ArrayList

时间:2014-09-13 14:37:56

标签: java android json arraylist

我使用HttpGet请求获取此对象,我希望将其添加到包含更多对象的ArrayList。解析整个对象的正确方法是什么?将它添加到数组中?

{
  "serial_number": "446D57D42F38",
  "name": "Garage Door",
  "accessories": [
    {
      "obfuscated": false,
      "target_state": "closed",
      "current_state": "opening",
      "name": "Garage Door Opener",
      "locked": false
    },
    {
      "hue": 55.5,
      "saturation": 85.5,
      "name": "Light Bulb",
      "power": true,
      "brightness": 95
    }
  ]
}

代码:

List<String> accessories = new ArrayList<String>();

HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
InputStreamReader text = new InputStreamReader(content, "UTF-8");
accessories.add(text.toString())

1 个答案:

答案 0 :(得分:3)

实例化JSONArray,然后创建for循环。在循环内调用jsonarray内的对象。