java 解析json字符串




Java中用于生成和解释json的主流工具有org.json、json-lib与gson


使用org.json解析json字符串时  请看此博文:http://blog.csdn.net/jediael_lu/article/details/25779087

org.json 使用起来比较简单


当使用json-lib时  请看此博文:http://www.cnblogs.com/sanhuan/p/4708554.html


运行这个类需要加载jar包:ezmorph-1.0.6.jar、json-lib-2.4-jdk15.jar、jsoup-1.6.1.jar、commons-beanutils-1.8.0.jar、commons-collections.jar、commons-lang-2.4.jar、commons-logging-1.1.1.jar。


commons-beanutils-1.8.0.jar不加这个包 
java.lang.NoClassDefFoundError: org/apache/commons/beanutils/DynaBean 
commons-collections.jar 不加这个包 
java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap 
commons-lang-2.4.jar不加这个包 
java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException 
commons-logging-1.1.1.jar不加这个包 
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 
ezmorph-1.0.6.jar不加这个包 
java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher 
json-lib-2.4-jdk15.jar不加这个包 
java.lang.NoClassDefFoundError: net/sf/json/JSONObject 
相应jar包可到网上下载

 这几个jar包一定要都有 不然编译时不报错,到进行解析字符串时真的报错

 

package src.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class TestJson
{
    // json字符串转
    private static String diskListContent =
        "[{\"n1\":\"asd\",\"n2\":22,\"n3\":\"45.40GB\"," + "\"n4\":\"qwerty\",\"n5\":\"asd\",}," + "{\"n1\":\"local\","
            + "\"n2\":1,\"n3\":\"279.40GB\",\"n4\":\"ST3300656SS\",\"n5\":\"\\/devm\\/d0\"}]";
    /***
     * json字符串转java List
     * @param rsContent
     * @return
     * @throws Exception
     */
    private static List<Map<String, String>> jsonStringToList(String rsContent) throws Exception
    {
        JSONArray arry = JSONArray.fromObject(rsContent);
        System.out.println("json字符串内容如下");
        System.out.println(arry);
        List<Map<String, String>> rsList = new ArrayList<Map<String, String>>();
        for (int i = 0; i < arry.size(); i++)
        {
            JSONObject jsonObject = arry.getJSONObject(i);
            Map<String, String> map = new HashMap<String, String>();
            for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();)
            {
                String key = (String) iter.next();
                String value = jsonObject.get(key).toString();
                map.put(key, value);
            }
            rsList.add(map);
        }
        return rsList;
    }
    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception
    {
        List<Map<String, String>> list1 = jsonStringToList(diskListContent);
        System.out.println("json字符串成map");
        for (Map<String, String> m : list1)
        {
            System.out.println(m);
        }
        System.out.println("map转换成json字符串");
        for (Map<String, String> m : list1)
        {
            JSONArray jsonArray = JSONArray.fromObject(m);
            System.out.println(jsonArray.toString());
        }
        System.out.println("list转换成json字符串");
        JSONArray jsonArray2 = JSONArray.fromObject(list1);
        System.out.println(jsonArray2.toString());
    }
}



已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页