json转换

使用的是json-lib.jar包

将json格式的字符数组转为List对象

 

  1.  
    package hb;  
  2.  
      
  3.  
    import java.util.Date;  
  4.  
      
  5.  
    public class Person {  
  6.  
          
  7.  
        String id;  
  8.  
        int age;  
  9.  
        String name;  
  10.  
        Date birthday;  
  11.  
        public String getId() {  
  12.  
            return id;  
  13.  
        }  
  14.  
        public void setId(String id) {  
  15.  
            this.id = id;  
  16.  
        }  
  17.  
        public int getAge() {  
  18.  
            return age;  
  19.  
        }  
  20.  
        public void setAge(int age) {  
  21.  
            this.age = age;  
  22.  
        }  
  23.  
        public String getName() {  
  24.  
            return name;  
  25.  
        }  
  26.  
        public void setName(String name) {  
  27.  
            this.name = name;  
  28.  
        }  
  29.  
        public Date getBirthday() {  
  30.  
            return birthday;  
  31.  
        }  
  32.  
        public void setBirthday(Date birthday) {  
  33.  
            this.birthday = birthday;  
  34.  
        }  
  35.  
          
  36.  
    }  
  1.  
    package hb;  
  2.  
      
  3.  
    import java.util.Iterator;  
  4.  
    import java.util.List;  
  5.  
      
  6.  
    import org.junit.Test;  
  7.  
      
  8.  
    import net.sf.json.JSONArray;  
  9.  
    import net.sf.json.JsonConfig;  
  10.  
      
  11.  
    public class JsonToList {  
  12.  
      
  13.  
        public static void main(String[] args) {  
  14.  
            String json="[{'name':'huangbiao','age':15},{'name':'liumei','age':14}]";  
  15.  
            JSONArray jsonarray = JSONArray.fromObject(json);  
  16.  
            System.out.println(jsonarray);  
  17.  
            List list = (List)JSONArray.toCollection(jsonarray, Person.class);  
  18.  
            Iterator it = list.iterator();  
  19.  
            while(it.hasNext()){  
  20.  
                Person p = (Person)it.next();  
  21.  
                System.out.println(p.getAge());  
  22.  
            }  
  23.  
        }  
  24.  
          
  25.  
        @Test  
  26.  
        public void jsonToList1(){  
  27.  
            String json="[{'name':'huangbiao','age':15},{'name':'liumei','age':14}]";  
  28.  
            JSONArray jsonarray = JSONArray.fromObject(json);  
  29.  
            System.out.println(jsonarray);  
  30.  
            List list = (List)JSONArray.toList(jsonarray, Person.class);  
  31.  
            Iterator it = list.iterator();  
  32.  
            while(it.hasNext()){  
  33.  
                Person p = (Person)it.next();  
  34.  
                System.out.println(p.getAge());  
  35.  
            }  
  36.  
              
  37.  
        }  
  38.  
          
  39.  
        @Test  
  40.  
        public void jsonToList2(){  
  41.  
            String json="[{'name':'huangbiao','age':15},{'name':'liumei','age':14}]";  
  42.  
            JSONArray jsonarray = JSONArray.fromObject(json);  
  43.  
            System.out.println(jsonarray);  
  44.  
            System.out.println("------------");  
  45.  
            List list = (List)JSONArray.toList(jsonarray, new Person(), new JsonConfig());  
  46.  
            Iterator it = list.iterator();  
  47.  
            while(it.hasNext()){  
  48.  
                Person p = (Person)it.next();  
  49.  
                System.out.println(p.getAge());  
  50.  
            }  
  51.  
              
  52.  
        }  
  53.  
      
  54.  
    }  

将list对象转为JSON字符串数组

  1.  
    package hb;  
  2.  
      
  3.  
    import java.util.LinkedList;  
  4.  
    import java.util.List;  
  5.  
      
  6.  
    import net.sf.json.JSONArray;  
  7.  
      
  8.  
    public class ListToJson {  
  9.  
      
  10.  
        public static void main(String[] args) {  
  11.  
            List list = new LinkedList();  
  12.  
            for(int i=0;i<3;i++){  
  13.  
                Person p = new Person();  
  14.  
                p.setAge(i);  
  15.  
                p.setName("name"+i);  
  16.  
                list.add(p);  
  17.  
            }  
  18.  
            JSONArray jsonarray = JSONArray.fromObject(list);  
  19.  
            System.out.println(jsonarray);  
  20.  
        }  
  21.  
      
  22.  
    }  

 打印结果

Java代码  收藏代码

  1. [{"age":0,"birthday":null,"id":"","name":"name0"},{"age":1,"birthday":null,"id":"","name":"name1"},{"age":2,"birthday":null,"id":"","name":"name2"}]  
posted @ 2020-04-17 15:00  兵兵的花园  阅读(133)  评论(0编辑  收藏  举报