使用GSON从Json解析为Java对象

时间:2016-12-17 09:34:36

标签: java json gson

我有JSON的一些谎言:

String json = "{'zadanie' : { "+
        "'lista' : {" +
            "'img': 'something.jpg'," +
            "'opis': 'Prawdziwy minionek'," +
            "'cena': 117.00," +
            "'ilosc': 12," +
            "'baza': ['truskawka - wanilia', 'sernik - pomarańcza', 'jagoda - wanilia', 'żurawina - wanilia', 'czekolada - wanilia', 'czekolada - banan', 'czekolada - cappuccino', 'tiramisu']," +
            "'czas': '2 dni'"+
        "}"+
        "',lista2' : {" +
            "'img': 'something2.jpg'," +
            "'opis': 'Prawdziwy minionek'," +
            "'cena': 117.00," +
            "'ilosc': 12," +
            "'baza': ['truskawka - wanilia', 'sernik - pomarańcza', 'jagoda - wanilia', 'żurawina - wanilia', 'czekolada - wanilia', 'czekolada - banan', 'czekolada - cappuccino', 'tiramisu']," +
            "'czas': '2 dni'"+
        "}"+
     "]}}";

我想用Java解析它。我有课程:

class Lista2{
private List<Lista> lista2;

    public List<Lista> getLista2() {
        return lista2;
    }

    public void setLista2(List<Lista> lista2) {
        this.lista2 = lista2;
    }

    @Override
    public String toString() {
        return "GroupList{" + "lista2=" + lista2 + '}';
    }}

//=================================================
class Lista {
private Data lista;

    public Data getLista() {
        return lista;
    }

    public void setLista(Data lista) {
        this.lista = lista;
    }

    @Override
    public String toString() {
        return "Lista{" + "lista=" + lista + '}';
    }}

//=================================================
class Data {
private String img;
private String opis;
private Double cena;
private Integer ilosc;
private List<String>  baza;
private String czas;

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public String getOpis() {
        return opis;
    }

    public void setOpis(String opis) {
        this.opis = opis;
    }

    public Double getCena() {
        return cena;
    }

    public void setCena(Double cena) {
        this.cena = cena;
    }

    public Integer getIlosc() {
        return ilosc;
    }

    public void setIlosc(Integer ilosc) {
        this.ilosc = ilosc;
    }

    public List<String> getBaza() {
        return baza;
    }

    public void setBaza(List<String> baza) {
        this.baza = baza;
    }

    public String getCzas() {
        return czas;
    }

    public void setCzas(String czas) {
        this.czas = czas;
    }

    @Override
    public String toString() {
        return "Data{" + "img=" + img + ", opis=" + opis + ", cena=" + cena + ", ilosc=" + ilosc + ", baza=" + baza + ", czas=" + czas + '}';
    } }

出了点问题,因为我有错误:&#34; StandardWrapperValve [Glowna]:Servlet的Servlet.service()Glowna抛出异常 com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:第1行第360列的未终止对象     在com.google.gson.Gson.fromJson(Gson.java:809)&#34;

对于GSON解析:

Gson gson = new Gson();
JsonReader reader = new JsonReader(new StringReader(json));
reader.setLenient(true);
Lista2 userinfo1 = gson.fromJson(reader, Lista2.class);

0 个答案:

没有答案