使用Json,可以下载如下所示的6个Jar包

 

整个工程目录结构如下:

 

简单的用法:

package json;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonTest {

    public static void main(String[] args) {
        //1、输出Json字符串
        JSONObject container1 = new JSONObject();
        container1.put("ClassName", "1班");
        System.out.println(container1.toString());
        
        //2、输出Json字符串 + Json数组
        JSONArray className =new JSONArray();
        className.add("2班");
        className.add("3班");
        container1.put("classname", className);
        System.out.println(container1.toString());
    
        //3、输出Json字符串 + Json数组 + Json字符串
        JSONObject classInfo = new JSONObject();
        classInfo.put("stuCount", 50);
        classInfo.put("leader", "rah");
        container1.put("classInfo", classInfo);
        System.out.println(container1);
        
        //4、字符串 转为 Json数组对象
        String jsonMessage = 
                "[{'num':'成绩', '外语':88, '历史':65, '地理':99, 'object':{'aaa':'1111','bbb':'2222','cccc':'3333'}}," +
                   "{'num':'兴趣', '外语':28, '历史':45, '地理':19, 'object':{'aaa':'11a11','bbb':'2222','cccc':'3333'}}," +
                   "{'num':'爱好', '外语':48, '历史':62, '地理':39, 'object':{'aaa':'11c11','bbb':'2222','cccc':'3333'}}]";
        
        JSONArray test = JSONArray.fromObject(jsonMessage);
        System.out.println(test.toString());
        //获取Json数组的某个索引的值
        System.out.println(test.get(0));
        System.out.println(test.get(1));
    }
    
}

 

 posted on 2017-10-10 14:44  布鲁布鲁sky  阅读(404)  评论(0编辑  收藏  举报