Fastjson 使用實例

2022-05-06 15:59 更新

1.fastJson 坐標(biāo)

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.39</version>
</dependency>

2.實例

package junit.test;  
  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
import com.alibaba.fastjson.JSON;  
import com.alibaba.fastjson.TypeReference;  

public class TestFastJson {  
      
    static class Person{          
        private String id ;  
        private String name;  
        private int age ;  
          
        public Person(){  
              
        }  
        public Person(String id,String name,int age){  
            this.id=id;  
            this.name=name;  
            this.age=age;  
        }  
        public String getId() {  
            return id;  
        }  
        public void setId(String id) {  
            this.id = id;  
        }  
        public String getName() {  
            return name;  
        }  
        public void setName(String name) {  
            this.name = name;  
        }  
        public int getAge() {  
            return age;  
        }  
        public void setAge(int age) {  
            this.age = age;  
        }  
        @Override  
        public String toString() {  
            return "Person [age=" + age + ", id=" + id + ", name=" + name + "]";  
        }  
          
          
    }  
    public static void main(String[] args) {  
        method1();  
        method2();  
        method3();  
        method4();  
    }  
      
    static void method1(){  
        System.out.println("javabean轉(zhuǎn)化示例開始----------");  
        Person person = new Person("1","fastjson",1);  
          
        //這里將javabean轉(zhuǎn)化成json字符串  
        String jsonString = JSON.toJSONString(person);  
        System.out.println(jsonString);  
        //這里將json字符串轉(zhuǎn)化成javabean對象,  
        person =JSON.parseObject(jsonString,Person.class);  
        System.out.println(person.toString());  
          
        System.out.println("javabean轉(zhuǎn)化示例結(jié)束----------");  
    }  
      
    static void method2(){  
        System.out.println("List<javabean>轉(zhuǎn)化示例開始----------");  
          
        Person person1 = new Person("1","fastjson1",1);  
        Person person2 = new Person("2","fastjson2",2);  
        List<Person> persons = new ArrayList<Person>();  
        persons.add(person1);  
        persons.add(person2);  
        String jsonString = JSON.toJSONString(persons);  
        System.out.println("json字符串:"+jsonString);  
          
        //解析json字符串  
        List<Person> persons2 = JSON.parseArray(jsonString,Person.class);  
        //輸出解析后的person對象,也可以通過調(diào)試模式查看persons2的結(jié)構(gòu)  
        System.out.println("person1對象:"+persons2.get(0).toString());  
        System.out.println("person2對象:"+persons2.get(1).toString());  
          
        System.out.println("List<javabean>轉(zhuǎn)化示例結(jié)束----------");  
    }  
}
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號