将整个 JSON 对象添加到 ArrayList [英] Add whole JSON object to ArrayList

查看:27
本文介绍了将整个 JSON 对象添加到 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 HttpGet 请求获得了这个对象,我想将它添加到具有更多对象的 ArrayList 中.解析整个对象并将其添加到数组的正确方法是什么?

I got this object using a HttpGet request that I'd like to add to an ArrayList with more objects. What would be the correct way to parse the whole object add it to the array?

{
  "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())

推荐答案

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

Instantiate a JSONArray and then create a for loop. Call the objects inside the jsonarray inside the loop.

这篇关于将整个 JSON 对象添加到 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆