gson JSON 到 Java - json 属性 有 .或者 - 在其中
gson JSON to Java -- json property has . or - in it
哪些步骤会重现问题?
1.I 有 json 响应
String Content =
"{
"region":{
"state.code":"TX",
"country-code":"USA"
}
}";
2.I想把这个Json对象转换成JavaObject.I有这个Javaclass转换
public class Region{
private String stateCode;
private String countryCode;
public String getStateCode ()
{
return stateCode;
}
public void setStateCode (String stateCode)
{
this.stateCode = stateCode;
}
public String getCountryCode ()
{
return countryCode;
}
public void setCountryCode (String countryCode)
{
this.countryCode = countryCode;
}
}
3.My 问题是 Java 不允许。或 - 在变量 name.Which 中不允许 json 字符串映射到区域 java class 对象给出 null .
gson.fromJson(Content, Region.class);
预期的输出是什么?你看到了什么呢?
有人可以帮我吗?
我已经尝试过 @SerializedName 注释,但它不起作用。
您使用的是什么版本的产品?在什么操作系统上?
请在下方提供任何其他信息。
您正在尝试映射此 JSON
{
"region":{
"state.code":"TX",
"country-code":"USA"
}
}
到类型 Region
的实例。 JSON 是一个 JSON 对象,它包含一个名称为 region
的对和一个 JSON 对象的值。 JSON 对象有两对,一对名称为 state.code
和 JSON 字符串值 TX
,另一对名称为 country-code
和 JSON 字符串值 USA
。
您不能将其映射到 Region
对象。您必须绕过名为 region
.
的根对
创建封装类型
class RegionContainer {
private Region region;
// the rest
}
并映射
gson.fromJson(Content, RegionContainer.class);
@SerializedName
将适用于您的领域。
@SerializedName("state.code")
private String stateCode;
哪些步骤会重现问题? 1.I 有 json 响应
String Content =
"{
"region":{
"state.code":"TX",
"country-code":"USA"
}
}";
2.I想把这个Json对象转换成JavaObject.I有这个Javaclass转换
public class Region{
private String stateCode;
private String countryCode;
public String getStateCode ()
{
return stateCode;
}
public void setStateCode (String stateCode)
{
this.stateCode = stateCode;
}
public String getCountryCode ()
{
return countryCode;
}
public void setCountryCode (String countryCode)
{
this.countryCode = countryCode;
}
}
3.My 问题是 Java 不允许。或 - 在变量 name.Which 中不允许 json 字符串映射到区域 java class 对象给出 null .
gson.fromJson(Content, Region.class);
预期的输出是什么?你看到了什么呢? 有人可以帮我吗? 我已经尝试过 @SerializedName 注释,但它不起作用。
您使用的是什么版本的产品?在什么操作系统上?
请在下方提供任何其他信息。
您正在尝试映射此 JSON
{
"region":{
"state.code":"TX",
"country-code":"USA"
}
}
到类型 Region
的实例。 JSON 是一个 JSON 对象,它包含一个名称为 region
的对和一个 JSON 对象的值。 JSON 对象有两对,一对名称为 state.code
和 JSON 字符串值 TX
,另一对名称为 country-code
和 JSON 字符串值 USA
。
您不能将其映射到 Region
对象。您必须绕过名为 region
.
创建封装类型
class RegionContainer {
private Region region;
// the rest
}
并映射
gson.fromJson(Content, RegionContainer.class);
@SerializedName
将适用于您的领域。
@SerializedName("state.code")
private String stateCode;