GSON fromJson 没有为 Java 字段赋值
GSON fromJson is not assigning values to the Java fields
我有一个 Json 字符串 "{"value":"3A72fd4ccb-1980-26cf-8db3-9eaadf1205c2"}"
,
同时将相同的字符串传递给以下代码:
Gson headerGson = new GsonBuilder().create();
Object ob = headerGson.fromJson(jsonStr, cl);
结果对象未分配给 "value" is json 字符串。
当我尝试使用 ReflectionAPI 打印对象字段时,我得到:
这些字段是:值
对应字段类型 --> class java.lang.String
对应的值为--> null
我在fromJson方法中作为"cl"传递的javaclass如下:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AttributedURI", propOrder = {
"value"
})
public class AttributedURI {
@XmlValue
@XmlSchemaType(name = "anyURI")
protected String value;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
/**
* Gets the value of the value property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getValue() {
return value;
}
/**
* Sets the value of the value property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setValue(String value) {
this.value = value;
}
/**
* Gets a map that contains attributes that aren't bound to any typed property on this class.
*
* <p>
* the map is keyed by the name of the attribute and
* the value is the string value of the attribute.
*
* the map returned by this method is live, and you can add new attribute
* by updating the map directly. Because of this design, there's no setter.
*
*
* @return
* always non-null
*/
public Map<QName, String> getOtherAttributes() {
return otherAttributes;
}
}
请让我知道我在这里遗漏了什么。
Class AttributedURI
代表你的json的内部:{"value":"3A72fd4ccb-1980-26cf-8db3-9eaadf1205c2"}
。所以,当你执行 methodheaderGson.fromJson(jsonStr, cl)
时,Gson 试图在 AttributedURI
class 中找到 MessageID
字段,这显然是不存在的。
要反序列化您的 json,您可以将 AttributedURI
包装在其他 class 中。例如:
public class OuterClass {
@SerializedName("MessageID")
private AttributedURI messageID;
public AttributedURI getMessageID(){
return messageID;
}
}
我也怀疑你在这里需要 XML 注释:afaik,GSON 作品不需要它们。
另外,Gson#fromJson
是泛型方法,所以可以这样写:
OuterClass ob = headerGson.fromJson(jsonStr, OuterClass.class);
我有一个 Json 字符串 "{"value":"3A72fd4ccb-1980-26cf-8db3-9eaadf1205c2"}"
,
同时将相同的字符串传递给以下代码:
Gson headerGson = new GsonBuilder().create();
Object ob = headerGson.fromJson(jsonStr, cl);
结果对象未分配给 "value" is json 字符串。 当我尝试使用 ReflectionAPI 打印对象字段时,我得到: 这些字段是:值 对应字段类型 --> class java.lang.String 对应的值为--> null
我在fromJson方法中作为"cl"传递的javaclass如下:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AttributedURI", propOrder = {
"value"
})
public class AttributedURI {
@XmlValue
@XmlSchemaType(name = "anyURI")
protected String value;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();
/**
* Gets the value of the value property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getValue() {
return value;
}
/**
* Sets the value of the value property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setValue(String value) {
this.value = value;
}
/**
* Gets a map that contains attributes that aren't bound to any typed property on this class.
*
* <p>
* the map is keyed by the name of the attribute and
* the value is the string value of the attribute.
*
* the map returned by this method is live, and you can add new attribute
* by updating the map directly. Because of this design, there's no setter.
*
*
* @return
* always non-null
*/
public Map<QName, String> getOtherAttributes() {
return otherAttributes;
}
}
请让我知道我在这里遗漏了什么。
Class AttributedURI
代表你的json的内部:{"value":"3A72fd4ccb-1980-26cf-8db3-9eaadf1205c2"}
。所以,当你执行 methodheaderGson.fromJson(jsonStr, cl)
时,Gson 试图在 AttributedURI
class 中找到 MessageID
字段,这显然是不存在的。
要反序列化您的 json,您可以将 AttributedURI
包装在其他 class 中。例如:
public class OuterClass {
@SerializedName("MessageID")
private AttributedURI messageID;
public AttributedURI getMessageID(){
return messageID;
}
}
我也怀疑你在这里需要 XML 注释:afaik,GSON 作品不需要它们。
另外,Gson#fromJson
是泛型方法,所以可以这样写:
OuterClass ob = headerGson.fromJson(jsonStr, OuterClass.class);