gpt4 book ai didi

json - 使用 GSON 的 JSON Java 对象给出计算器错误

转载 作者:行者123 更新时间:2023-12-04 02:09:34 25 4
gpt4 key购买 nike

我正在尝试使用 GSON 将列表格式的 java 对象转换为 JSON,我尝试了几种方法,但遇到了同样的错误。

第一次尝试

List<Techinv> techs = UserUtil.getTechModels(group, org);
Gson gson = new Gson();
String json = gson.toJson(techs);

第二次尝试

List<Techinv> techs = UserUtil.getTechModels(group, org);
Type listType = new TypeToken<List<Techinv>>() {}.getType();
Gson gson = new Gson();
String json = gson.toJson(techs, listType);

第三次尝试

List<Techinv> techs = UserUtil.getTechModels(group, org);
GsonBuilder gsonBuilder = new GsonBuilder();

new GraphAdapterBuilder()
.addType(Techinv.class)
.registerOn(gsonBuilder);
Gson gson = gsonBuilder.create();
String json = gson.toJson(techs);

上述所有操作均失败,出现stackoverflow 错误无法将 java int 转换(我的 Techinv 类中的成员之一)为 java 向量, 都在 GSON 库中。

请告诉我,在所有这些示例中我遗漏了一些简单的东西:)

Techinv 类(如果需要)

package org.cchit.inv.model;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;


/**
* The persistent class for the techinv database table.
*
*/
@Entity
@NamedNativeQueries ({
@NamedNativeQuery(name="Techinv.deleteByOrgId",
query="DELETE FROM techinv where org_id = ?")
})
@NamedQueries ({
@NamedQuery(name="Techinv.removeUser",
query="UPDATE Techinv t SET user_id = 0 where t.id = :techid"),
@NamedQuery(name="Techinv.getAllByOrg",
query="SELECT p FROM Techinv p where p.organization.liferayId = :orgid"),
@NamedQuery(name="Techinv.getById",
query="SELECT t FROM Techinv t where t.id = :id"),
@NamedQuery(name="Techinv.getByProdOrg",
query="SELECT p FROM Techinv p where p.organization.liferayId = :orgid and p.product.id = :prodid"),
@NamedQuery(name="Techinv.delete",
query="DELETE FROM Techinv t where t.id = :id")
})
@Table(name="techinv")
public class Techinv implements Serializable {
private static final long serialVersionUID = 1L;

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

@Column(name="mu_mask")
private long muMask;

@Column(name="mu_assigned_mask")
private long muAssignedMask;

@Column(name="mu_asked_assign_mask")
private long muAskedAssignedMask;

@Column(name="mu_cert_mask")
private long muWillCertMask;

@Column(name="mu_ask_will_cert_mask")
private long muAskedWillCertMask;

@Column(name="certType")
private String certType;

@Temporal(TemporalType.DATE)
@Column(name="Apply_date")
private Date applyDate;

@Temporal(TemporalType.DATE)
@Column(name="Cert_date")
private Date certDate;

@Lob()
private String notes;

//bi-directional many-to-one association to OrgUser
@ManyToOne
@JoinColumn(name="user_id")
private OrgUser orgUser;

//bi-directional many-to-one association to Organization
@ManyToOne
@JoinColumn(name="org_id")
private Organization organization;

//bi-directional many-to-one association to Product
@ManyToOne
@JoinColumn(name="prod_id")
private Product product;

//bi-directional many-to-one association to Certification
@ManyToOne
@JoinColumn(name="cert_id")
private Certification certification;

public void setCertification(Certification certification) {
this.certification = certification;
}

public Product getProduct() {
return product;
}

public void setProduct(Product product) {
this.product = product;
}

public Techinv() {
}

public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}

public String getNotes() {
return this.notes;
}

public void setNotes(String notes) {
this.notes = notes;
}

public OrgUser getOrgUser() {
return this.orgUser;
}

public void setOrgUser(OrgUser orgUser) {
this.orgUser = orgUser;
}

public Organization getOrganization() {
return this.organization;
}

public void setOrganization(Organization organization) {
this.organization = organization;
}

public long getMuMask() {
return muMask;
}

public void setMuMask(long muMask) {
this.muMask = muMask;
}

public long getMuAssignedMask() {
return muAssignedMask;
}

public void setMuAssignedMask(long muAssigned) {
this.muAssignedMask = muAssigned;
}

public Certification getCertification() {
return certification;
}

public long getMuWillCertMask() {
return muWillCertMask;
}

public void setMuWillCertMask(long muWillCertMask) {
this.muWillCertMask = muWillCertMask;
}

public long getMuAskWillCertMask() {
return muAskedWillCertMask;
}

public void setMuAskedWillCertMask(long muAskedWillCertMask) {
this.muAskedWillCertMask = muAskedWillCertMask;
}

/**
* This will set the question to true. Once asked and answered, this cannot be unset.
* @param mu
*/
public void setMuAskedAssignedMask(Mu mu) {
this.muAskedAssignedMask |= mu.getMask();
}

public boolean isCertified(Mu mu) {
return getCertification() != null && (getCertification().getMuMask() & mu.getMask()) > 0;
}

public boolean isAssigned(Mu mu) {
return (getMuAssignedMask() & mu.getMask()) > 0;
}

public boolean hasAskedToCertify(Mu mu) {
return isAssigned(mu) && !isCertified(mu) && (getMuAskWillCertMask() & mu.getMask()) > 0;
}

public boolean isWillCertify(Mu mu) {
return hasAskedToCertify(mu) && (getMuWillCertMask() & mu.getMask()) > 0;
}

public boolean hasMu(Mu mu) {
return (getMuMask() & mu.getMask()) > 0;
}

public boolean hasAskedToAssign(Mu mu) {
return (muAskedAssignedMask & mu.getMask()) > 0;
}

public String getCertType() {
return certType;
}

public void setCertType(String certType) {
this.certType = certType;
}

public Date getApplyDate() {
return applyDate;
}

public void setApplyDate(Date applyDate) {
this.applyDate = applyDate;
}

public Date getCertDate() {
return certDate;
}

public void setCertDate(Date certDate) {
this.certDate = certDate;
}
}

最佳答案

您应该检查序列化类中的循环引用并标记适当的属性@Transient。如果您需要这方面的帮助,请发布您的 TechModel 类(class)。

编辑:因为使用@Transient 不是您的选择,请使用 Gson 的@Expose。来自GSON user guide :

This feature provides a way where you can mark certain fields of your objects to be excluded for consideration for serialization and deserialization to JSON. To use this annotation, you must create Gson by using new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(). The Gson instance created will exclude all fields in a class that are not marked with @Expose annotation.

关于json - 使用 GSON 的 JSON Java 对象给出计算器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13092727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com