Java解析Json数据

方式一:使用json.lib.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

/**
 * 使用json.lib.jar解析json数据
 */</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> parseJsonWithJsonLib(){
    String jsonString = "<span style="color: #8b0000">{\"Image\":{\"Width\":800,\"Height\":600,\"Title\":\"View from 15th Floor\",\"Thumbnail\":{\"Url\":\"http://www.example.com/image/481989943\",\"Height\":125,\"Width\":\"100\"},\"IDs\":[116, 943, 234, 38793]}}</span>";
    JSONObject jsonObject = JSONObject.fromObject(jsonString);
    JSONObject jsonObjectImage = jsonObject.getJSONObject("<span style="color: #8b0000">Image</span>");
    String width = jsonObjectImage.getString("<span style="color: #8b0000">Width</span>");
    String height = jsonObjectImage.getString("<span style="color: #8b0000">Height</span>");
    String title = jsonObjectImage.getString("<span style="color: #8b0000">Title</span>");
    System.out.println("<span style="color: #8b0000">WIDTH:</span>" + width);
    System.out.println("<span style="color: #8b0000">HEIGHT:</span>" + height);
    System.out.println("<span style="color: #8b0000">TITLE:</span>" + title);

    JSONObject jsonObjectTHU = jsonObjectImage.getJSONObject("<span style="color: #8b0000">Thumbnail</span>");
    String url = jsonObjectTHU.getString("<span style="color: #8b0000">Url</span>");
    String width2 = jsonObjectTHU.getString("<span style="color: #8b0000">Width</span>");
    String height2 = jsonObjectTHU.getString("<span style="color: #8b0000">Height</span>");
    System.out.println("<span style="color: #8b0000">URL:</span>" + url);
    System.out.println("<span style="color: #8b0000">WIDTH2:</span>" + width2);
    System.out.println("<span style="color: #8b0000">HEIGHT2:</span>" + height2);

    JSONArray jsonArrayIDS = jsonObjectImage.getJSONArray("<span style="color: #8b0000">IDs</span>");
    <span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> i=0;i<jsonArrayIDS.size();i++){
        System.out.println("<span style="color: #8b0000">元素</span>" + i + "<span style="color: #8b0000">:</span>" + jsonArrayIDS.getInt(i));
    }
}</pre>


方式二:使用org.json.jar解析

/**
     * 使用org.json.jar解析json数据
     */
    public static void parseJsonWithOrgJson(){
        try {
            String jsonString = "{\"Image\":{\"Width\":800,\"Height\":600,\"Title\":\"View from 15th Floor\",\"Thumbnail\":{\"Url\":\"http://www.example.com/image/481989943\",\"Height\":125,\"Width\":\"100\"},\"IDs\":[116, 943, 234, 38793]}}";
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONObject jsonObjectImage = jsonObject.getJSONObject("Image");
            String width = jsonObjectImage.getString("Width");
            String height = jsonObjectImage.getString("Height");
            String title = jsonObjectImage.getString("Title");
            System.out.println("WIDTH:" + width);
            System.out.println("HEIGHT:" + height);
            System.out.println("TITLE:" + title);

            JSONObject jsonObjectTHU = jsonObjectImage.getJSONObject("Thumbnail");
            String url = jsonObjectTHU.getString("Url");
            String width2 = jsonObjectTHU.getString("Width");
            String height2 = jsonObjectTHU.getString("Height");
            System.out.println("URL:" + url);
            System.out.println("WIDTH2:" + width2);
            System.out.println("HEIGHT2:" + height2);

            JSONArray jsonArrayIDS = jsonObjectImage.getJSONArray("IDs");
            for(int i=0;i

  

  
   
 
   
 元素" + i + "
 
   
 
   :" + jsonArrayIDS.getInt(i));
            }
        } 
 
   
 
   catch (JSONException e) {
            
 
   
 
   // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

  

  


两种方式对比:两种解析方式基本相同,但是org.json不需要依赖其他包,比json.lib轻量。

你可能感兴趣的:(Java解析Json数据)