FastJson解析实例--对象序列化和反序列化

前端之家收集整理的这篇文章主要介绍了FastJson解析实例--对象序列化和反序列化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

直接上源码...注释中有详解

package com.company;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

    public static void main(String[] args) {
        //这里将javabean转化成json字符串
        Person person = new Person("1","fastjson",1);
        String jsonString = JSON.toJSONString(person);
        System.out.println("将javabean转化成json字符串:"+jsonString);

        //这里将json字符串转化成javabean对象,Person personSec = new Person();
        personSec = JSON.parSEObject(jsonString,Person.class);
        System.out.println("将json字符串转化成javabean对象:"+personSec.getName());


        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 jsonStringSec = JSON.toJSONString(persons);//是json数组
        System.out.println("将List<JavaBean>转换成json字符串:"+jsonStringSec);

       // List<Person> persons2 = JSON.parseArray(jsonStringSec,Person.class);  ///这两种方法均可
        List<Person> persons2 = JSON.parSEObject(jsonStringSec,new TypeReference<List<Person>>(){});///这两种方法均可
        System.out.println("将json字符串转换成List<JavaBean>:"+persons2.get(1).getName());


        List<String> list = new ArrayList<String>();
        list.add("fastjson1");
        list.add("fastjson2");
        list.add("fastjson3");
        String jsonStringThird = JSON.toJSONString(list);//是json数组
        System.out.println("将list转换为json字符串:"+jsonStringThird);

        List<String> list2 = JSON.parSEObject(jsonStringThird,new TypeReference<List<String>>(){});
        System.out.println("将json转换为List:"+list2.get(2));

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("key1","val1");
        map.put("key2","val2");
        Map<String,Object> map2 = new HashMap<String,Object>();
        map2.put("key1",1);
        map2.put("key2",2);
        List<Map<String,Object>>  list3 = new ArrayList<Map<String,Object>>();
        list3.add(map);
        list3.add(map2);
        String jsonStringFourth = JSON.toJSONString(list3);
        System.out.println("将List<Map<String,Object>>对象转化成json字符串:"+jsonStringFourth);
        List<Map<String,Object>> list4 = JSON.parSEObject(jsonStringFourth,new TypeReference<List<Map<String,Object>>>(){});
        System.out.println("将json字符串转化成List<Map<String,Object>>对象"+list4.get(1).get("key1"));

    }

}
以上共分为4种情况。
package com.company;

/**
 * Created by liuyazhou on 2016/9/27.
 */
public class Person {

    private String id;
    private String name;
    private Integer num;

    public Person() {
    }

    public Person(String id,String name,Integer num) {
        this.id = id;
        this.name = name;
        this.num = num;
    }

    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 Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }
}
jar下载地址:https://github.com/alibaba/fastjson
"C:\Program Files...
将javabean转化成json字符串:{"id":"1","name":"fastjson","num":1}
将json字符串转化成javabean对象:fastjson
将List<JavaBean>转换成json字符串:[{"id":"1","name":"fastjson1","num":1},{"id":"2","name":"fastjson2","num":2}]
将json字符串转换成List<JavaBean>:fastjson2
将list转换为json字符串:["fastjson1","fastjson3"]
将json转换为List:fastjson3
将List<Map<String,Object>>对象转化成json字符串:[{"key2":"val2","key1":"val1"},{"key2":2,"key1":1}]
将json字符串转化成List<Map<String,Object>>对象:1

Process finished with exit code 0

另外这是我个人开发的App,欢迎下载和好评,V1.5.0版本正在努力开发中......
http://www.wandoujia.com/apps/com.example.viewpager_3 原文链接:https://www.f2er.com/json/289186.html

猜你在找的Json相关文章