【问题标题】:Not able to get the values using Gson无法使用 Gson 获取值
【发布时间】:2014-12-02 12:48:53
【问题描述】:

我有 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
}

]

我想在这里获取标题和网址。你能建议我吗?

【问题讨论】:

  • 如果您想将它们从序列化过程中排除,您可以将该字段标记为瞬态或使用@Expose 注解。

标签: java json list gson


【解决方案1】:

您必须将transient 修饰符添加到您不想被序列化的类中的成员。

  // will be serialized
  private String propertyOne;

  // will not be serialized
  private transient String propertyTwo;

【讨论】:

    【解决方案2】:

    如果只需要特定字段,可以在这些字段上使用@Exposesee 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);
    

    【讨论】:

    • 这个Userdetails是他的bean类还是什么?这里我要写Annotation什么的/
    • @mdrazi 那么userDetailFeedNotifierVoList 的数据类型是什么?
    • 列表
    • @mdrazi 所以UserDetail 相当于你的userDetailFeedNotifierVoList 类。
    • 我有提到... List userDetailFeedNotifierVoList = null; @ZouZou
    猜你喜欢
    • 1970-01-01
    • 2020-05-23
    • 2017-05-12
    • 2017-07-29
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 2013-03-22
    相关资源
    最近更新 更多