json转 list Gson.fromJson 转list json数组转list java json数组转list

maven 地址

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.6</version>
</dependency>

json数组转 list

public class Parite {
    private String cz;
    private String oe;

    public String getCz() {
        return cz;
    }

    public void setCz(String cz) {
        this.cz = cz;
    }

    public String getOe() {
        return oe;
    }

    public void setOe(String oe) {
        this.oe = oe;
    }

    @Override
    public String toString() {
        return "Parite{" +
                "cz='" + cz + '\'' +
                ", oe='" + oe + '\'' +
                '}';
    }
}
public class JSonToList {
    public static void main(String[] args) {
        String str = "[{\"cz\": \"2\",\"oe\": \"521190X935\"},{\"cz\": \"3\",\"oe\": \"521190X936\"}]";
        Gson gson = new Gson();
        List<Parite> list = gson.fromJson(str,  new TypeToken<List<Parite>>() {}.getType());
        for (Parite p:list){
            System.out.println(p.toString());
        }
    }
}

Parite{cz='2', oe='521190X935'}
Parite{cz='3', oe='521190X936'}

猜你喜欢

转载自blog.csdn.net/yu1xue1fei/article/details/115214347