Map (就一个json.jar)

    public static void main(String[] args) {
        List<Map<Integer, String>> m = new ArrayList<Map<Integer,String>>();
        Map<Integer, String> i = new HashMap<Integer, String>();//    {1=1, 111=111, 1111=1111, 11=11}
        i.put(1, "1");
        i.put(11, "11");
        i.put(111, "111");
        i.put(1111, "1111");
        Map<Integer, String> ii = new HashMap<Integer, String>();//    {1=1, 111=111, 1111=1111, 11=11}
        ii.put(1, "1");
        ii.put(11, "11");
        ii.put(111, "111");
        ii.put(1111, "1111");
        m.add(i);
        m.add(ii);//    [{1=1, 111=111, 1111=1111, 11=11}, {1=1, 111=111, 1111=1111, 11=11}]
    }

 


      从外到里看,例子中就是一个数组,数组里面是两个json格式的字符串。这样分析思路就清晰多了。

    工作中需要取出name4的值,你们会怎么取呢?。最初我都想过字符串截取,那时还不了解JSONArray,现在知道了,取出来也就相当容易了。


    取出name4值过程步骤:1,将以上字符串转换为JSONArray对象;2,取出对象的第一项,JSONObject对象;3,取出name1的值 JSONObject对象;4,取出name2的值JSONObject对象;5,取出name4的值value2。

    示例中json数组格式的字符串可以通过方法直接转换为JSONArray的格式:JSONArray.fromObject(String)

 

  1. </pre><pre name="code" class="java">JSONArray getJsonArray=JSONArray.fromObject(arrayStr);//将结果转换成JSONArray对象的形式  
  2. JSONObject getJsonObj = getJsonArray.getJSONObject(0);//获取json数组中的第一项  
  3. String result=getJsonObj.getJSONObject("name1").getJSONObject("name2").getJSONObject("name4");  

 

 

   好了我们说说这两个对象。

1,JSONObject

 json对象,就是一个键对应一个值,使用的是大括号{ },如:{key:value}

2,JSONArray

  json数组,使用中括号[ ],只不过数组里面的项也是json键值对格式的


  Json对象中添加的是键值对,JSONArray中添加的是Json对象

 

  1. JSONObject Json = new JSONObject();  
  2. JSONArray JsonArray = new JSONArray();  
  3.   
  4. Json.put("key", "value");//JSONObject对象中添加键值对  
  5. JsonArray.add(Json);//将JSONObject对象添加到Json数组中  


3,JSONObject与Map

   Map map和json都是键值对,不同的是map中键值对中间用等号分开,json中键值对中间用冒号分开。其实json就是一种特殊形式的map。

 

 

 

import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class map {
    public static void main(String[] args) throws JSONException {

        // str是JSONObject
        String str = "{\"people\":[{\"firstName\":\"Brett\",\"lastName\":\"McLaughlin\",\"email\":\"aaaa\"},{\"firstName\":\"Jason\",\"lastName\":\"Hunter\",\"email\":\"bbbb\"},{\"firstName\":\"Elliotte\",\"lastName\":\"Harold\",\"email\":\"cccc\"}]}";
        JSONObject jo = new JSONObject(str);// {"people":[{"lastName":"McLaughlin","email":"aaaa","firstName":"Brett"},{"lastName":"Hunter","email":"bbbb","firstName":"Jason"},{"lastName":"Harold","email":"cccc","firstName":"Elliotte"}]}
        // getJSONArray()表示返回值是JSONArray
        JSONArray info = jo.getJSONArray("people");// [{"lastName":"McLaughlin","email":"aaaa","firstName":"Brett"},{"lastName":"Hunter","email":"bbbb","firstName":"Jason"},{"lastName":"Harold","email":"cccc","firstName":"Elliotte"}]
        System.out.println(info);
        for (int i = 0; i < info.length(); i++) {
            JSONObject obj = info.getJSONObject(i);// {"lastName":"McLaughlin","email":"aaaa","firstName":"Brett"}
            Iterator it = obj.keys();
            while (it.hasNext()) {
                String key = (String) it.next();// lastName
                System.out.println(obj.get(key));// McLaughlin
            }
        }

        // jsonContent是JSONObject
        String jsonContent = "{'hello':'world','abc':'xyz'}";
        JSONObject jsonObject = new JSONObject(jsonContent);// {"hello":"world","abc":"xyz"}
        String str1 = jsonObject.getString("hello");// world
        String str2 = jsonObject.getString("abc");// xyz
        System.out.println(str1);
        System.out.println(str2);

        // jsonContent是JSONArray
        jsonContent = "[{'hello':333,'abc':false,'xyz':{'a':1,'b':'ab'}},{'hello':555,'abc':true,'xyz':{'a':2,'b':'ba'}}]";
        JSONArray jsonArray = new JSONArray(jsonContent);// [{"hello":333,"abc":false,"xyz":{"b":"ab","a":1}},{"hello":555,"abc":true,"xyz":{"b":"ba","a":2}}]
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonobject2 = jsonArray.getJSONObject(i);// {"hello":333,"abc":false,"xyz":{"b":"ab","a":1}}
            int value1 = jsonobject2.getInt("hello");// 333
            boolean value2 = jsonobject2.getBoolean("abc");// false
            // String value3=jsonobject2.getString("xyz");
            JSONObject jsonobject3 = jsonobject2.getJSONObject("xyz");// {"b":"ab","a":1}
            int value4 = jsonobject3.getInt("a");// 1
            String value5 = jsonobject3.getString("b");// ab
            System.out.println(value1);
            System.out.println(value2);
            System.out.println(value4);
            System.out.println(value5);
        }

        // str是JSONObject,没有最外层key的json
        str = "{'TI':[{'value':'aa1','count':10},{'value':'aa2','count':15},{'value':'aa3','count':20}],"
                + "'AB':[{'value':'ab','count':110},{'value':'ab2','count':115},{'value':'ab3','count':210}]}";
        JSONArray newArray = new JSONArray();
        JSONObject newJson = new JSONObject();
        try {
            JSONObject obj = new JSONObject(str); // {"AB":[{"count":110,"value":"ab"},{"count":115,"value":"ab2"},{"count":210,"value":"ab3"}],"TI":[{"count":10,"value":"aa1"},{"count":15,"value":"aa2"},{"count":20,"value":"aa3"}]}
            Iterator it = obj.keys();
            while (it.hasNext()) {
                String key = (String) it.next(); // AB
                String value = obj.getString(key); // [{"count":110,"value":"ab"},{"count":115,"value":"ab2"},{"count":210,"value":"ab3"}]
                JSONArray array = obj.getJSONArray(key); // [{"count":110,"value":"ab"},{"count":115,"value":"ab2"},{"count":210,"value":"ab3"}]
                for (int i = 0; i < array.length(); i++) {
                    JSONObject jsonobject = array.getJSONObject(i); // {"count":110,"value":"ab"}
                    jsonobject.put("name", key);
                    jsonobject.put("exp",
                            key + "=" + jsonobject.getString("value"));
                    newArray.put(jsonobject); // {"exp":"AB=ab","count":110,"name":"AB","value":"ab"}
                }
            }
            newJson.put("groups", newArray);
            System.out.println(newJson); // {"groups":[{"exp":"AB=ab","count":110,"name":"AB","value":"ab"},{"exp":"AB=ab2","count":115,"name":"AB","value":"ab2"},{"exp":"AB=ab3","count":210,"name":"AB","value":"ab3"},{"exp":"TI=aa1","count":10,"name":"TI","value":"aa1"},{"exp":"TI=aa2","count":15,"name":"TI","value":"aa2"},{"exp":"TI=aa3","count":20,"name":"TI","value":"aa3"}]}

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}

 

posted @ 2016-05-05 11:24  无天666  阅读(402)  评论(0编辑  收藏  举报