JSON01_资料

1、

  资料网址:

    http://blog.csdn.net/vincent_czz/article/details/7333977

    http://blog.csdn.net/huangwuyi/article/details/5412500

  下载网址:https://sourceforge.net/projects/json-lib/files/

  在线API:http://json-lib.sourceforge.NET/apidocs/jdk15/index.html

2、

  异常处理:http://www.xuebuyuan.com/2158801.html

  2.1、异常:

    在“JSONObject.fromObject(...)”处报错:“Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException”

  2.2、处理:

    项目中导入了 json-lib 包,但是在运行如下代码的时候还是出现了如下错误,究其原因是因为缺包,缺了如下5个包:

      commons-beanutils-1.8.3
      commons-lang-2.6 (注:导入最新的 3.1 版本会继续报上面的错误)
      commons-collections-3.2.1
      commons-logging-1.1.1
      ezmorph-1.0.6

  2.3、我现在使用的相关包分别为:(它们 都是从各自的官网上下载下来的)

    (1)、json-lib-2.4-jdk15.jar
    (2)、commons-beanutils-1.8.3.jar
    (3)、commons-collections-3.2.2.jar
    (4)、commons-lang-2.6.jar
    (5)、commons-logging-1.2.jar
    (6)、ezmorph-1.0.6.jar

3、ZC 测试代码

package test;

import java.util.Iterator;
import net.sf.json.*;

public class Ttest03
{
    public static void main(String[] args)
    {
        String jsonString = "{"FLAG":"flag","MESSAGE":"SUCCESS","name":[{"name":"jack"},{"name":"lucy"}]}";  
        try
        {
            JSONObject result = JSONObject.fromObject(jsonString);//转换为JSONObject  
            JSONArray nameList = result.getJSONArray("name");//获取JSONArray  
            int iSize = nameList.size();
            System.out.println("iSize : "+iSize);
            
            String strName01 = "";  
            for(int i = 0; i < iSize; i++)//遍历JSONArray
            {
                JSONObject oj = nameList.getJSONObject(i);  
                strName01 = oj.getString("name");  
                System.out.println("["+Integer.toString(i)+"] :  name : "+strName01);  
            }
            Iterator<?> itKeys = result.keys();  
            String aa2 = "";  
            String bb2 = null;  
            while(itKeys.hasNext())//遍历JSONObject
            {
                bb2 = (String) itKeys.next().toString();  
                aa2 = result.getString(bb2);  
                System.out.println(bb2+" -- "+aa2);
            }
        //} catch (JSONException e) {
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

4、

5、

原文地址:https://www.cnblogs.com/javaskill/p/5973773.html