나는 http 클라이언트를 작성했으며 응답을 얻고있다. 나는 데이터를 구문 분석 된 JSONObject를 사용하고 있으며 여기 자바에서 JSON 응답 (목록 포함) 구문 분석
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if(entity!=null){
try(InputStream instream = entity.getContent()) {
String responseString = readInputStream(instream);
JSONObject job = new JSONObject(responseString);
statusLabel.setText("Command Result: " + job.toString());
가 readInputSream 기능 그냥 벌금 JSON의 모든 출력한다 아래 때이 코드를 실행 : 나는에서 그것을 변경하는 경우
static private String readInputStream(InputStream inputStream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"));
String tmp;
StringBuilder sb = new StringBuilder();
while ((tmp = reader.readLine()) != null) {
sb.append(tmp).append("\n");
}
if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\n') {
sb.setLength(sb.length() - 1);
}
reader.close();
return sb.toString();
}
을 에) (job.toString :
statusLabel.setText("Command Result: " + job.get("result"));
그것은을 my_list까지 모든 방법을 작동, 올 1을 인쇄합니다. 목록을 구문 분석하는 방법을 모르겠습니다. 아래 응답의 스 니펫을 넣습니다. 필자는 "을 my_list", "을 my_list []"을 my_list [0] 빈 ", 당신은 검색하려는 요소가 JSONArray에 구문 분석
{"result":1, "ver":1,"total":2,"catch":true,"my_list":[{"id":3,"mid":0,"format":3,"user":4,"property":1,"type":0,"title":"hello","start":146,"end":1464,"hid":3,"bid":1,"reason":1,"time":0,"creator":"1","hello":0,"my":0,"year":"0","ggg":614,"name":"","ch":"0","attr":0,"type":1,"vtype":1,"tm_log": {"fr":4,"action":0,"vr":"82","started":1,"av_ended":2,"tr":1}}
http://docs.oracle.com/javaee/7/api/javax/와 같은 것을 사용하는 대신 자신이 직접 읽으려고합니다. json/JsonReader.html # readObject-- – Reek