码迷,mamicode.com
首页 > 编程语言 > 详细

JsonTest.java

时间:2017-06-02 12:42:03      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:false   ini   sage   dmi   ota   trace   except   .json   creat   

//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

标签:false   ini   sage   dmi   ota   trace   except   .json   creat   

原文地址:http://www.cnblogs.com/tangyongathuse/p/6932634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!