//import SurveyVO;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import org.json.JSONString;import java.util.ArrayList;import java.util.List;/** * Created by Administrator on 2017/6/2. */public class JsonTest { public static void main(String[] args) { String sTotalString = "{\"message\":\"success\",\n" + " \"result\":[\n" + " {\"surveyId\":\"1\",\"surveyName\":\"B\"},\n" + " {\"surveyId\":\"2\",\"surveyName\":\"C\"}\n" + " ]}"; JSONObject json; try { json = new JSONObject(sTotalString); JSONArray results = json.getJSONArray("result"); List surveyVOList = new ArrayList(); for (int i = 0; i < results.length(); i++) { SurveyVO surveyVO = new SurveyVO(); JSONObject result = results.getJSONObject(i); System.out.println(result.getString("surveyId")+" "+result.getString("surveyName")); surveyVO.setSurveyId(result.getString("surveyId")); surveyVO.setSurveyName(result.getString("surveyName")); //System.out.println(surveyVO.toString()); surveyVOList.add(surveyVO); } //System.out.println(surveyVOList.toString()); } catch (JSONException e) { e.printStackTrace(); } }}
SurveyVO.java
/** * Created by Administrator on 2017/6/2. */public class SurveyVO { private String surveyId; private String surveyName; public String getSurveyId() { return surveyId; } public void setSurveyId(String surveyId) { this.surveyId = surveyId; } public String getSurveyName() { return surveyName; } public void setSurveyName(String surveyName) { this.surveyName = surveyName; }}