Gson反序列化返回所有空值[Java]

时间:2018-04-16 20:16:10

标签: java json serialization null gson

我的回复Json格式看起来像这样

background-repeat: repeat;

但是,当我使用Gson反序列化我的数据时,所有值都为null

这是我的代码,用于将JSON读取和反序列化为我的对象

[
  {
    "cpm": "$33.00",
    "id": 13125,
    "name": "zemindary, eichwaldite",
    "startDate": "2018-01-04"
  },
  {
    "cpm": "$9.00",
    "id": 652,
    "name": "polyandrian self-exciter",
    "startDate": "2018-01-04"
  }
]

这是我的数据模型:     公共类活动{

Gson g = new Gson();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder content = new StringBuilder();
while ((inputLine = in.readLine()) != null ) {
    content.append(inputLine);
}
Campaigns[] newCamp = g.fromJson(content.toString(), Campaigns[].class);
for (int i = 0; i < newCamp.length; i++) {
    Document doc = Document.createDocument("id", newCamp[i].getId())
        .put("cpm", newCamp[i].getCpm())
        .put("name", newCamp[i].getName())
        .put("startDate", newCamp[i].getStartDate());
    collection.insert(doc);
    System.out.println(doc);
}

以下是我在打印数据模型时所获得的内容

  

{id = 14789,cpm = null,name = null,startDate = null,_id = 20548426011802,_revision = 1,_modified = 1523897108160}

0 个答案:

没有答案