JSON解析与GSON返回null [英] parse JSON with GSON returns null

查看:283
本文介绍了JSON解析与GSON返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的JSON:

[
{
"canaldsc":"PHARMA",
"cadenadsc":null,
"formatodsc":"DEL AHORRO",
"estadodsc":null,
"ciudaddsc":"VALLE DE MEXICO",
"regiondsc":"CENTRO SUR",
"proyectotiendaid":null,
"proyectoid":"79",
"planid":"54",
"proyectodsc":"Concurso",
"vigencia":"0000-00-00",
"activo":"1",
"usuarioproyectotiendaid":"65280",
"proyecto":null,
"fechainicio":"2014-03-24",
"lunes":"7",
"martes":"7",
"miercoles":"7",
"jueves":"7",
"viernes":"7",
"sabado":"7",
"domingo":"7"
},
{
"canaldsc":"PHARMA",
"cadenadsc":null,
"formatodsc":"DEL AHORRO",
"estadodsc":null,
"ciudaddsc":"VALLE DE MEXICO",
"regiondsc":"CENTRO SUR",
"proyectotiendaid":null,
"proyectoid":"79",
"planid":"54",
"proyectodsc":"Concurso",
"vigencia":"0000-00-00",
"activo":"1",
"usuarioproyectotiendaid":"65284",
"proyecto":null,
"fechainicio":"2014-03-24",
"lunes":"7",
"martes":"7",
"miercoles":"7",
"jueves":"7",
"viernes":"7",
"sabado":"7",
"domingo":"7"
}
]

在现实中,有2个以上的对象,但我把它简化

In reality, there is more than 2 objects, but I cut it to simplify

我的模型类:

public class PdvResult {
public PdvResult() {
}

public String canaldsc;
public String cadenadsc;
public String formatodsc;
public String estadodsc;
public String ciudaddsc;
public String regiondsc;
public String proyectotiendaid;
public String proyectoid;
public String planid;
public String proyectodsc;
public String vigencia;
public String activo;
public String usuarioproyectotiendaid;
public String proyecto;
public String fechainicio;
public String lunes;
public String martes;
public String miercoles;
public String jueves;
public String viernes;
public String sabado;
public String domingo;
}

它应该在的名称对应,使得映射可以发生

It should be corresponds in the names so that the mapping can happen.

在我的Java,我用

List<PdvResult> pdvs = (List<PdvResult>)gson.fromJson(reader, new TypeToken<PdvResult>() {}.getType());

但pdvs返回null。

but pdvs return null.

这有什么错我的code ????

What's wrong with my code????

推荐答案

更改,你解析JSON行了,用这个:

Change the line where you parse the JSON, use this:

List<PdvResult> pdvs = gson.fromJson(reader, new TypeToken<List<PdvResult>>() {}.getType());

您正在使用 TypeToken&LT; PdvResult&GT; ,你必须使用 TypeToken&LT;名单&LT; PdvResult&GT;&GT; ,否则你想你的JSON解析成 PdvResult 对象,而你实际上有 PdvResult 的数组对象!

You are using TypeToken<PdvResult>, and you have to use TypeToken<List<PdvResult>>, otherwise you're trying to parse your JSON into a PdvResult object, while you actually have an array of PdvResult objects!

这篇关于JSON解析与GSON返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆