【问题标题】:How convert POJO to BSON with Google GSON?如何使用 Google GSON 将 POJO 转换为 BSON?
【发布时间】:2015-06-03 15:55:33
【问题描述】:

要使用 Jackson 将 POJO 转换为 BSON,我知道 bson4jackson (http://www.michel-kraemer.com/binary-json-with-bson4jackson)。是否有与 Google GSON 相同的功能?

提前致谢。

【问题讨论】:

    标签: gson


    【解决方案1】:

    检查这个question

    我已经使用过 gson:

    • 转换为 pojo:

      Gson gsonObj= new Gson();

      MyPojo mypojoObj = gsonObj.fromJson( someJsonString, MyPojo.class);

    • 然后转成json字符串:

      String s=gsonObj.toJson(mypojoObj);

    MyPojo 中所有参数的名称和 json 字符串也应该有 1-1 的对应关系。你可以测试一下。我相信只有匹配的才会被序列化。

    例如,如果:

    class MyPojo
    {
        public String xString;
        public String yString;
        ...
    }
    

    然后用:

    MyPojor= gsonObj.fromJson("{\"xString\":\"1\",\"yString\":\"1\",\"zString\":\"1\"}", MyPojor.class);
    
    return gsonObj.toJson(r);
    

    你得到:

    {"xString":"1","yString":"1"}
    

    您可以测试不要求序列化参数的情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多