我可能会得到一个json响应
"contentId": "1234",
"events": [{
"value": "night"
}]
and sometimes I get as
"contentId": "1235",
"events": [{
"value": true
}]
I am using GSON parsing ,
@Expose
private Boolean value;
public Boolean getValue() {
return value;
}
public void setValue(Boolean value) {
this.value = value;
}
我如何获得字符串? 我想解析字符串和布尔值,但两者都有相同的名称"值"。我怎样才能进行解析?
答案 0 :(得分:1)
假设您在Java代码中将所有"value"
内的值作为String
获取,则可以使用以下内容:
boolean myVal;
if (valueFromJson.equals("true")) {
myVal = true;
// implement whatever logic here which uses value as boolean type
} else if (valueFromJson.equals("false")) {
myVal = false;
// implement whatever logic here which uses value as boolean type
} else {
// use logic where you need the String value
}
注意:这个假设字符串的值永远不会是" true"或"假"工作。