无法使用Gson获取值

时间:2014-12-02 12:48:54

标签: java json list gson

我有List Object。这个列表我要传递给Gson。

        Gson gson = new Gson();
        String toJson = gson.toJson(userDetailFeedNotifierVoList);
        System.out.println("Gson Object : " + toJson);

我在这里进入控制台:

[
{
    "title": "Suman Mudiraj assigned you a to-do item named test todo in the Launch GALAXY activity.",
    "time": "2014-10-08T10:51:07.095Z",
    "url": "https://greenhouse.lotus.com/activities/service/html/mainpage#activitypage,0c7b1fb5-a87f-4112-9b7f-67ed6bd0233f,entry=634e9535-ba17-40da-a242-bd9caff6f17d",
    "schedullarTime": 0
},
{
    "title": "Suman Mudiraj assigned you a to-do item named test todo in the Launch GALAXY activity.",
    "time": "2014-10-08T10:51:07.095Z",
    "url": "https://greenhouse.lotus.com/activities/service/html/mainpage#activitypage,0c7b1fb5-a87f-4112-9b7f-67ed6bd0233f,entry=634e9535-ba17-40da-a242-bd9caff6f17d",
    "schedullarTime": 0
}

我想获得头衔和网址。可以请你推荐我吗?

2 个答案:

答案 0 :(得分:1)

如果您只需要特定字段,则可以在以下字段中使用@Expose see gson docs

class UserDetailFeedNotifierVo
{
   @Expose
   String title;

   @Expose
   String url;

   //not exposed
   String time;
}

使用它:

Gson gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
String toJson = gson.toJson(userDetailFeedNotifierVoList);
System.out.println("Gson Object : " + toJson);

答案 1 :(得分:1)

您必须向班级中不想要序列化的成员添加transient修饰符。

  // will be serialized
  private String propertyOne;

  // will not be serialized
  private transient String propertyTwo;