zoukankan      html  css  js  c++  java
  • 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"}]  
  • 相关阅读:
    年轻人的第一个 Spring Boot 应用,太爽了!
    面试问我 Java 逃逸分析,瞬间被秒杀了。。
    Spring Boot 配置文件 bootstrap vs application 到底有什么区别?
    坑爹的 Java 可变参数,把我整得够惨。。
    6月来了,Java还是第一!
    Eclipse 最常用的 10 组快捷键,个个牛逼!
    Spring Cloud Eureka 自我保护机制实战分析
    今天是 Java 诞生日,Java 24 岁了!
    厉害了,Dubbo 正式毕业!
    Spring Boot 2.1.5 正式发布,1.5.x 即将结束使命!
  • 原文地址:https://www.cnblogs.com/lbbk/p/12720063.html
Copyright © 2011-2022 走看看