![](/img/trans.png)
[英]How to obtain the same result with google-Gson and JSON.stringfy
[英]Java can't parse JSON generated by JSON.stringfy
从rc-1升级到rc-3后, JSON.stringfy()
方法在每个值的开头和结尾都返回带有\\
的值:
{
\"perfil\":\"CLIENTE\", ...
}
我该如何解决?
代码段:
post(url, data) {
console.log(JSON.stringify(data));
return Observable.create(observer =>
this.http.post(this.restConfig.baseUrl + url, JSON.stringify(data), {
headers: this.getDefaultHeaders()
}).subscribe(
data => this.next(observer, data)
, err => {
console.log(err);
if (err.status === 401) {
this.redirectAuth();
}
observer.error(err);
}
)
);
}
我的Java RESTful服务无法解析输出:
Unexpected token (VALUE_STRING), expected FIELD_NAME: missing property 'perfil' that is to contain type id (for class br.com.inbit.medipop.model.entities.impl.Cliente) at [Source: java.io.ByteArrayInputStream@79f844cf; line: 1, column: 1]
客户类:
@Table
@Entity
@DiscriminatorValue("CLIENTE")
public class Cliente extends Usuario {
}
Usuario类:
@Table
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "perfil", discriminatorType = DiscriminatorType.STRING)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "perfil")
@JsonSubTypes({
@JsonSubTypes.Type(value = Administrador.class, name = "ADMIN"),
@JsonSubTypes.Type(value = Colaborador.class, name = "COLABORADOR"),
@JsonSubTypes.Type(value = Parceiro.class, name = "PARCEIRO"),
@JsonSubTypes.Type(value = Cliente.class, name = "CLIENTE"),
@JsonSubTypes.Type(value = Dependente.class, name = "DEPENDENTE")
})
public abstract class Usuario {
@NotNull
@Enumerated(EnumType.STRING)
@Column(insertable = false, updatable = false)
protected PerfilUsuario perfil;
...
}
stringfy
之前的数据:
{"perfil":"CLIENTE","pessoa":{"tipo":"FISICA","sexo":"MASCULINO","nome":"Marcos Kichel","cpf":"911.111.064-36","rg":"1234"},"dependentes":[],"email":"anackichel@gmail.com"}
似乎您正在对已经进行了字符串化的内容进行字符串化。 取出JSON.stringify(),您应该会很好。
这里的问题实际上不是由引号引起的,而是perfil
字段的数据类型错误。 在您的Java代码中,它应该是一个对象,而在json中,您传递字符串值"CLIENTE"
。
因此,请确保您在“ perfil”字段中发送对象,或者将perfil
字段的类型从PerfilUsuario
为String
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.