【发布时间】:2017-12-18 19:06:40
【问题描述】:
我有一个类,其中包含一些字段和额外的字符串类型数据,将存储一个 json 对象。我希望它不被反序列化,但是找了一个多小时后,我唯一发现的是:
@Expose(deserialize=false)
但它不起作用。
以下是一个例子:
public class Employee implements Serializable
{
private static final long serialVersionUID = 1L;
@Expose
public Long id;
@Expose
public String name;
... some more fields
@SerializedName("extraData")
@Expose(deserialize = false)
public String extraData;
}
然后在我的服务中,我执行以下操作:
final GsonBuilder builder = new GsonBuilder();
final Gson gson = builder.create();
Employee emp = gson.fromJson(json, Employee.class);
我从前端收到的对象是:
{
"id":1,
"name": "John Doe",
"extraData": {"someField1":"someValue1","someField2":"someValue2"}
}
我得到这个错误: java.lang.IllegalStateException:应为字符串,但在第 1 行第 48 列路径 $.extraData 处为 BEGIN_OBJECT
当我只想将“extraData”作为具有值的字符串获取时
{"someField1":"someValue1","someField2":"someValue2"}
不接受其他解决方案,因为这是我给出的规范。
【问题讨论】:
-
怎么样?
"{\"someField1\":\"someValue1\",\"someField2\":\"someValue2\"}"
标签: java json serialization gson