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的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数