【问题标题】:Gson.toJson(Object) returning object hash codeGson.toJson(Object) 返回对象哈希码
【发布时间】:2014-10-15 04:54:10
【问题描述】:

我正在尝试使用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? 提前致谢。

【问题讨论】:

  • 为什么有类型适配器?为什么要将toString 的输出写入JsonWriter?
  • @SotiriosDelimanolis 如果没有类型适配器,Gson 会给我一个 StackOverFlow。 gson.ToJson(user) 的输出是一个 Restfull 方法的返回。
  • Review 对象是否有对其User 的反向引用?
  • 您知道toString 方法的工作原理吗?它的目的是什么?
  • 这是一个危险的游戏。阅读here。

标签: java json gson


【解决方案1】:

我认为您需要使用方法 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();

【讨论】:

  • 很抱歉,没有结果。我也没有在我的 User 类上实现 Serializable 接口。也许这就是问题所在?
  • 尝试添加Serializable接口,有什么问题?如果可以,请不要忘记在您的 User 类上添加 private static final long serialVersionID。
  • 是的...抱歉耽搁了,但还是一样。现在我将分解 User 对象的属性,然后逐步形成另一个 Json。
  • 覆盖 toString() 方法。它应该将您的 User 类打印为 JSON 数组。我认为经过此操作后,您将在所有程序中获得正确的输出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 2017-10-16
  • 1970-01-01
  • 2017-12-30
相关资源
最近更新 更多