简体   繁体   English

GSON映射POJO属性的空值

[英]GSON mapping null value for POJO properties

I'm trying to use GSON 2.2.2 (for the very first time) to map JSON into a Java POJO. 我正在尝试使用GSON 2.2.2(第一次)将JSON映射到Java POJO中。 I'm hitting a 3rd party RESTful web service and this is an example of the JSON I'm getting back: 我正在使用第三方RESTful Web服务,这是我返回的JSON的示例:

{
    "response": {
        "job":{
            "eta":-1,
            "status":"approved",
            "mt":1,
            "lc_tgt":"fr",
            "body_src":"Please translated me.",
            "body_tgt":"S'il vous plaît traduire moi.",
            "unit_count":3,
            "tier":"machine",
            "credits":0,
            "ctime":"2013-02-07 14:56:12.391963",
            "lc_src":"en",
            "slug":"0",
            "job_id":"NULL"
        }
    },
    "opstat":"ok"
}

The POJO I'm trying to map this into is: 我正在尝试将其映射到的POJO是:

public class Job {
    // correlates to "eta"
    private int eta;

    // correlates to "body_src"
    private String sourceBody;

    // correlates to "ctime"
    private java.util.Date creationTimestamp;

    // Getters and setters for all 3 properties
}

When I run the following code, I don't get any exceptions, but the print statement just prints " null ": 当我运行以下代码时,没有任何异常,但是print语句仅显示“ null ”:

// Hit the 3rd party service and get the JSON (example above).
JSONObject json = hitRestfulWebService();

Gson gson = new Gson();

// json.toString = "{response":{"job":{ ..."
Job job = gson.fromJson(json.toString(), Job.class);

System.out.println(job.getSourceBody());

My only guess is that GSON can't figure out how to map the 3 JSON fields to my 3 Job properties. 我唯一的猜测是GSON无法弄清楚如何将3个JSON字段映射到我的3个Job属性。 Can someone help me figure out what this mapping needs to be? 有人可以帮我弄清楚这个映射需要是什么吗? Thanks in advance. 提前致谢。

You can use annotations to define, which json field gets mapped to which object member, eg: 您可以使用注释来定义哪个json字段映射到哪个对象成员,例如:

class SomeClass
{
   @SerializedName("body-src")
   String myString1;

   @SerializedName("header-src")
   String myString2;
...

use not response but response.job 不使用response但使用response.job

not

  {   "response": {..

use 采用

 { "eva": ..

this may help; 这可能会有所帮助;

    String a = "{\"response\": {\"job\":{\"eta\":-1,\"status\":\"approved\",\"mt\":1,\"lc_tgt\":\"fr\",\"body_src\":\"Please translated me.\",\"body_tgt\":\"S'il vous plaît traduire moi.\",\"unit_count\":3,\"tier\":\"machine\",\"credits\":0,\"ctime\":\"2013-02-07 14:56:12.391963\",\"lc_src\":\"en\",\"slug\":\"0\",\"job_id\":\"NULL\"}},\"opstat\":\"ok\"}";



    Job j = I.gson().fromJson(
            ((JsonObject) ((JsonObject) new JsonParser().parse(a)).get("response")).get("job"), Job.class);

    System.out.println(j.getEta());
public class Response{
  private Job job;

  //generate setter and getter
}

public class Job {
    // correlates to "eta"
    private int eta;

    // correlates to "body_src"
    private String sourceBody;

    // correlates to "ctime"
    private java.util.Date creationTimestamp;

    // Getters and setters for all 3 properties
}

now in Gson 现在在格森

JSONObject json = hitRestfulWebService();

Gson gson = new Gson();

// json.toString = "{response":{"job":{ ..."
Job job = gson.fromJson(json.toString(), Response.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM