2014-09-13 3 views
0

더 많은 객체가있는 ArrayList에 추가하려는 HttpGet 요청을 사용하여이 객체를 가져 왔습니다. 전체 개체를 구문 분석하는 올바른 방법은 무엇입니까 배열에 추가 할?ArrayList에 전체 JSON 객체 추가

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

왜 jsonarray를 사용하지 않습니까? EntityUtils를 사용하여 문자열 -json directlty를 얻을 수 있습니다. – Nikhil

+0

@Nikhil 정보를 제공해 주셔서 감사합니다! – Kenny

답변

3

은 JSONArray를 인스턴스화 한 후 루프를 만들 수 있습니다. 루프 안에서 jsonarray 내부의 객체를 호출하십시오.