Java 将整个JSON对象添加到ArrayList
我使用一个HttpGet请求获得了这个对象,我想将它添加到一个包含更多对象的ArrayList中。解析整个对象并将其添加到数组中的正确方法是什么Java 将整个JSON对象添加到ArrayList,java,android,json,arraylist,Java,Android,Json,Arraylist,我使用一个HttpGet请求获得了这个对象,我想将它添加到一个包含更多对象的ArrayList中。解析整个对象并将其添加到数组中的正确方法是什么 { "serial_number": "446D57D42F38", "name": "Garage Door", "accessories": [ { "obfuscated": false, "target_state": "closed", "current_state": "opening
{
"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
}
]
}
代码:
列出附件=新的ArrayList();
HttpResponse response=client.execute(httpGet);
HttpEntity=response.getEntity();
InputStream内容=entity.getContent();
InputStreamReader文本=新的InputStreamReader(内容,“UTF-8”);
附件。添加(text.toString())
实例化JSONArray,然后创建for循环。在循环内调用jsonarray内的对象。实例化jsonarray,然后创建for循环。在循环内调用jsonarray内的对象。实例化jsonarray,然后创建for循环。在循环内调用jsonarray内的对象。实例化jsonarray,然后创建for循环。在循环中调用jsonarray中的对象。为什么不使用jsonarray?您可以使用EntityUtils获取字符串jsondirectlty@Nikhil我会的,谢谢你的信息!为什么不使用jsonarray呢?您可以使用EntityUtils获取字符串jsondirectlty@Nikhil我会的,谢谢你的信息!为什么不使用jsonarray呢?您可以使用EntityUtils获取字符串jsondirectlty@Nikhil我会的,谢谢你的信息!为什么不使用jsonarray呢?您可以使用EntityUtils获取字符串jsondirectlty@Nikhil我会的,谢谢你的信息!
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())