JSON:fastjson

一.JSONObject

1.Encode
import java.util.ArrayList;
import java.util.List;

class Group{
    private Long id;
    private String name;
    private List<User> users=new ArrayList<>();

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public void addUser(User user){
        this.users.add(user);
    }
}

class User{
    private Long id;
    private String name;

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }
}
public class JsonOp1 {
    public static void main(String[] args) {
        Group group=new Group();
        group.setId(0L);
        group.setName("admin");

        User user1=new User();
        user1.setId(1L);
        user1.setName("Mike");

        User user2=new User();
        user2.setId(2L);
        user2.setName("Bob");

        group.addUser(user1);
        group.addUser(user2);

        String jsonStr=JSON.toJSONString(group);
        System.out.println(jsonStr);
    }
}

//{"id":0,"name":"admin","users":[{"id":1,"name":"Mike"},{"id":2,"name":"Bob"}]}
2.Decode
import java.util.ArrayList;
import java.util.List;

class Group {
    private Long id;
    private String name;
    private List<User> users = new ArrayList<>();

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public void addUser(User user) {
        this.users.add(user);
    }
}

class User {
    private Long id;
    private String name;

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class JsonOp1 {
    public static void main(String[] args) {
        String jsonStr = "{'id':0,'name':'admin','users':[{'id':1,'name':'Mike'},{'id':2,'name':'Bob'}]}";
        Group group = JSON.parseObject(jsonStr, Group.class);
        System.out.println(group.getId() + " " + group.getName() + " " + group.getUsers().get(0).getId() + " " + group.getUsers().get(0).getName() + group.getUsers().get(1).getId() + " " + group.getUsers().get(1).getName());
    }
}

二.JSONArray

1.Encode

import java.util.ArrayList;
import java.util.List;

class Computer {
    private String brand;
    private int price;

    public String getBrand() {
        return brand;
    }

    public int getPrice() {
        return price;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}

public class JsonOp4 {
    public static void main(String[] args) {
        List<Computer> computers = new ArrayList<>();
        Computer computer1 = new Computer();
        computer1.setBrand("lenovo");
        computer1.setPrice(5000);
        computers.add(computer1);

        Computer computer2 = new Computer();
        computer2.setBrand("apple");
        computer2.setPrice(10000);
        computers.add(computer2);

        String jsonStr = JSON.toJSONString(computers);
        System.out.println(jsonStr);
    }
}

 

2.Decode

import java.util.Iterator;
import java.util.List;

class Transport{
    private String name;
    private int price;

    public String getName() {
        return name;
    }

    public int getPrice() {
        return price;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}
public class JsonOp3 {
    public static void main(String[] args) {
        String jsonStr="[{'name':'subway','price':3},{'name':'bus','price':2}]";
        List<Transport> transports=JSON.parseArray(jsonStr,Transport.class);//用array的话就不用Gson的TypeToken
        Iterator<Transport> it=transports.iterator();
        while (it.hasNext()){
            Transport transport=it.next();
            System.out.println(transport.getName()+" "+transport.getPrice());
        }
    }
}

 

refhttps://github.com/alibaba/fastjson/wiki

0