JsonTest.java
Posted tangyongathuse
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JsonTest.java相关的知识,希望对你有一定的参考价值。
//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<SurveyVO>(); 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; } }
以上是关于JsonTest.java的主要内容,如果未能解决你的问题,请参考以下文章