- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用Gson.ToJson(Object)
方法转换下面的类,但它返回了我该类的对象哈希代码,例如:br.com .helpradar.entity.User@475fdaaa
但是,我可以毫无问题地检索 user.expertise 并获取所有关系:Gson.ToJson(user.getExpertise)
@Entity
@SequenceGenerator(name="seqUser", sequenceName="SEQ_USER", allocationSize=1)
public class User {
@Id
private Long id;
@Column(nullable=false)
private String name;
@OneToOne
private Contact contact;
//enum
private TypeUser typeUser;
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(name = "USER_REVIEW",
joinColumns = { @JoinColumn(name = "USER_ID") },
inverseJoinColumns = { @JoinColumn(name = "REVIEW_ID") })
@Column(name="REVIEW")
private Set<Review> review= new HashSet<Review>();
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(name = "USER_EXPERTISE",
joinColumns = { @JoinColumn(name = "USER_ID") },
inverseJoinColumns = { @JoinColumn(name = "EXPERTISE_ID") })
@Column(name="EXPERTISE")
private Set<Expertise> expertise = new HashSet<Expertise>();
}
这是我的 Gson 方法:
Gson gson = new GsonBuilder()
.registerTypeAdapter(User.class, new MyTypeAdapter<Expertise>())
.registerTypeAdapter(User.class, new MyTypeAdapter<Review>())
.create();
return gson.toJson(user);
这是我的 MyTypeAdapter:
class MyTypeAdapter<T> extends TypeAdapter<T> {
public T read(JsonReader reader) throws IOException {
return null;
}
public void write(JsonWriter writer, T obj) throws IOException {
if (obj == null) {
writer.nullValue();
return;
}
writer.value(obj.toString());
}
}
那么,如何让Gson.ToJson(user)
实际返回一个Json字符串,以便我可以在另一端使用Gson.FromJson?提前致谢。
最佳答案
我认为您需要使用方法enableComplexMapKeySerialization()。 Here您可以查看下一个文档:
public GsonBuilder enableComplexMapKeySerialization()
Enabling this feature will only change the serialized form if the map
key is a complex type (i.e. non-primitive) in its serialized JSON form.
The default implementation of map serialization uses toString() on the key...
示例:
Gson gson = new GsonBuilder()
.register(Point.class, new MyPointTypeAdapter())
.enableComplexMapKeySerialization()
.create();
Map<Point, String> original = new LinkedHashMap<Point, String>();
original.put(new Point(5, 6), "a");
original.put(new Point(8, 8), "b");
System.out.println(gson.toJson(original, type));
输出将是:
{
"(5,6)": "a",
"(8,8)": "b"
}
所以,你可以尝试这样:
Gson gson = new GsonBuilder()
.registerTypeAdapter(User.class, new MyTypeAdapter<Expertise>())
.registerTypeAdapter(User.class, new MyTypeAdapter<Review>())
.enableComplexMapKeySerialization()
.create();
关于java - Gson.toJson(Object) 返回对象哈希码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26374716/
还是两者兼而有之? 最佳答案 因为扩展Object是不好的做法。 关于javascript - 为什么 MongoDB 不扩展对象以能够使用 a.tojson() 但仍然需要 tojson(a)?,我
我在另一个答案中看到,为了包含虚拟字段,您必须执行类似 https://groups.google.com/forum/?fromgroups#!topic/mongoose-orm/HjrPAP_W
我正在尝试使用 Gson 的 toJson API 将我的对象转换为 JSON 字符串。当我遇到支持相同的 2 个不同的 API 时。 As per Docs - public String toJs
我是knockout js的新手,我想将knockout.js与asp.net表单应用程序一起使用。我想使用 ajax 调用更新实体,如下所示 // Update product details
我是 JSON 的新手,因此我不确定 $.toJSON(params) 是什么意思。 请解释这是做什么的。 最佳答案 可能是这个jQuery plugin var myObj = {}; myObj.
让我们举个例子。我有一个 pojo 类,如下所示。 public class MyRecord{ private String name; private String id;
我有这样的代码: var test = new Date(2019, 03, 15, 13, 15, 0); console.log(test); //outputs Mon Apr 15 2019
我不确定为什么 this.model 在我的主干 View 中未定义。 这是我的脚本: var TheModel = Backbone.Model.extend({ defaults: {
我编写的组件将带有\n 换行符的文本转换为 html 段落 文本.js const Text = props => (( { props.paragraph.split('\n').ma
我需要从 JSON 响应中删除某些字段。为此,我使用了 toJSON() 方法。这是我的模态方法的代码。 User.methods.toJSON = function () { l
我在 asp.net mvc 页面上使用 knockoutjs。我正在使用 ajax 通过调用 ko.toJSON(viewModel) 将表单保存回服务器。然后使用 jQuery 将结果发布回服务器
我正在尝试使用 jQuery 和 Ajax,并且使用此方法。但我收到错误 $.toJSON is not a function in my firebug 。哪里有问题?我使用 jquery 1.3.
第一 quote来自 Redux 文档: It is highly recommended that you only put plain serializable objects,arrays, a
我创建了一个 .NET core 2.2 webapi,并使用 swagger/nswag 为我的 React/typescript 应用程序生成 API。当我尝试设置一个新对象时,我得到一个 ts(
例如,我位于澳大利亚,而 new Date() 为我提供澳大利亚的当前日期和时间2016 年 8 月 26 日星期五 09:16:16 GMT+1000(澳大利亚东部标准时间),如果我写 new Da
listData.forEach(function (row, index) { console.log(row); --> output JSON format { _id: 5
我对 knockout 计算的可观察值和 toJSON 函数有疑问。我创建了一个Fiddle Example 。在此示例中,我有一个模型: function VM() { this.Name =
我正在使用 knockout 映射插件从我的服务器映射对象。 我想重新发送这个对象,因此,我最终需要将这个对象转换为字符串。我在计算函数中使用了 knockout 函数 ko.toJSON,以便隐藏字
我正在尝试获取一些数据并使用 Backbone 显示它,但运气不佳 - 我没有收到任何错误,只是没有渲染(我使用 todos 示例作为指导构建了这个非常简单的应用程序) index.html:
我有一个 javascript 对象,如下所示: var Person = function() { var _name = ""; this.Age = 0; Object.
我是一名优秀的程序员,十分优秀!