json-lib基礎

1、json-lib所需的jar包:java

  json-lib.jar,commons-beanutils.jar,commons-collections.jar,commons-lang.jar,commons-logging.jar,ezmorph-1.0.6.jar(可直接在網上搜索下載)。json

 

2、解析轉化:spa

  一、json字符串  <------>  jsoncode

     String jsonStr="{\"name\" : \"peter\",\"age\" : 25,\"address\" : \"廣州\",\"weight\" : \"55.5\",\"handsome\" : true}";
     //String------>json JSONObject jsonObject
=JSONObject.fromObject(jsonStr);
     //json------>String
     String jsonString=jsonObject.toString();

 

  二、java實體類  <------>  jsonblog

public class User {
    private String name;
    private int age;
    private String address;
    private double weight;
    private boolean handsome;
    //省略構造和get/set方法
}
  User user=new User("peter", 20, "廣州", 55.5, true);
  //java實體類------>json JSONObject jsonObject
=JSONObject.fromObject(user);

  

  
//{"weight":55.5,"address":"廣州","name":"peter","age":20,"handsome":true} System.out.println(jsonObject);

   

  三、List  <------>  jsonrem

        List<String> list=new ArrayList<String>();
        list.add("QQQQQQ");
        list.add("WWWWWW");
        list.add("EEEEEE");
        list.add("RRRRRR");
        //List------>json
        JSONArray jsonArray=JSONArray.fromObject(list); 
        //json------>List
        List<String> list3=(List)JSONArray.toCollection(jsonArray);

 

  四、Map  <------>  json字符串

     Map<String, String> map=new HashMap<String, String>();
        map.put("name", "peter");
        map.put("sex", "Man");
        map.put("age", "20");

        //Map------json
        JSONObject jsonMap=JSONObject.fromObject(map);
        //json------>Map
        Map<String, String> mapJson=(Map)JSONObject.toBean(jsonMap, Map.class);

 

  五、Set  <------>  jsonget

     Set<String> set=new HashSet<String>();
        set.add("QQQQQQ");
        set.add("wwwwww");
        set.add("eeeeee");
        //Set------>json
        JSONArray array=JSONArray.fromObject(set);
        
        //json------>Set
        Set<String> set1=new HashSet<String>();
        for (Object object : array) {
            set1.add(object.toString());
        }
        
        //報錯:java.lang.ClassCastException
        Set<String> setJson=(Set<String>)JSONArray.toCollection(array,Set.class);
        
        //直接轉List
        List<String> list=(List)JSONArray.toCollection(array);

 

3、CRUD:io

  一、查詢數據:ast

     //對比age和weight數據
        String jsonStr="{\"name\" : \"peter\",\"age\" : 25,\"address\" : \"廣州\",\"weight\" : \"55.5\",\"handsome\" : true}";
        //String------>json
        JSONObject jsonObject=JSONObject.fromObject(jsonStr);
        String name=jsonObject.getString("name");
        String age=jsonObject.getString("age");
        String address=jsonObject.getString("address");
        String handsome=jsonObject.getString("handsome");
        
        //正常輸出25
        int age1=jsonObject.getInt("age");
        long age2=jsonObject.getLong("age");
        double age3=jsonObject.getDouble("age");
        
        //int和long輸出55,double輸出55.5
        int weight1=jsonObject.getInt("age");
        long weight2=jsonObject.getLong("age");
        double weight3=jsonObject.getDouble("age");
        
        boolean handsome1=jsonObject.getBoolean("handsome");
        for (int i = 0; i < jsonArray.size(); i++) {
            System.out.println(jsonArray.get(i));
        }

 

  二、增長數據

    jsonObject.put("height", 170);
        jsonArray.add("MMMMMM");

 

  三、刪除數據:

      jsonObject.remove("height");
        //index從0開始
        jsonArray.remove(index);

 

  四、修改數據:

        //重置數據便可
        jsonObject.put("age", 20);
        for (int i = 0; i < jsonArray.size(); i++) {
            if("QQQQQQ".equals(jsonArray.get(i))){
                jsonArray.remove(i);
                jsonArray.add("qqqqqq");
            }
        }
相關文章
相關標籤/搜索