instanceof 不适用于 JSON 对象 - 为什么?
instanceof not working for a JSON object - why?
您好,在下面的代码中,键名作为值充当对象和字符串。想要检查名称。如果名称与之匹配,那么它应该检查两个条件值是对象还是字符串 .
如果值是对象,则如果条件应该执行,否则条件应该执行。但它不会进入任何循环。
谁能告诉我我哪里做错了
Json 响应:
{
"name": "account_id",
"value": {
"value": "11x52927",
"label": "Alfa HOSPITAL"
},
"label": "Account Name",
"uitype": "51",
"type": {
"defaultValue": null
}
},
{
"name": "cf_905",
"value": "Intensive Care Medicine",
"label": "Specialization",
"uitype": "16",
"type": {
"defaultValue": null
}
},
Contacts.java:
for (SynFields synFields1: synFields) {
String name = synFields1.getName();
if (name.equals("account_id")) {
Object values = synFields1.getValue();
try {
if (values == JSONObject.NULL) {
// Handle NULL
} else if (values instanceof JSONObject) {
JSONObject jsonObject1 = null;
try {
jsonObject1 = new
JSONObject(String.valueOf(synFields1.getValue()));
String value = ((JSONObject) values).getString("value");
String labels = ((JSONObject) values).getString("label");
account_names.add(labels);
} catch (JSONException e) {
e.printStackTrace();
}
} else {
String value_accounts = String.valueOf(synFields1.getValue());
account_names.add(value_accounts);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
SynField.java:
public class SynFields {
@SerializedName("name")
@Expose
private String name;
@SerializedName("value")
@Expose
private Object value;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
@SerializedName("label")
@Expose
private String label;
}
您可以使用 Gson
将 Object
转换为 JSONObject
。
这样试试
try {
if (values == Null) {
// Handle null object
continue;
}
if(values instanceof String){
String value_accounts = String.valueOf(values);
account_names.add(value_accounts);
}else{
Gson gson = new Gson();
JSONObject jsonObject = new JSONObject(gson.toJson(values)); // convert Object to JSONObject
String value = jsonObject.getString("value");
String labels = jsonObject.getString("label");
account_names.add(labels);
}
} catch (JSONException e) {
e.printStackTrace();
}
如果值是对象,则如果条件应该执行,否则条件应该执行。但它不会进入任何循环。
谁能告诉我我哪里做错了
Json 响应:
{
"name": "account_id",
"value": {
"value": "11x52927",
"label": "Alfa HOSPITAL"
},
"label": "Account Name",
"uitype": "51",
"type": {
"defaultValue": null
}
},
{
"name": "cf_905",
"value": "Intensive Care Medicine",
"label": "Specialization",
"uitype": "16",
"type": {
"defaultValue": null
}
},
Contacts.java:
for (SynFields synFields1: synFields) {
String name = synFields1.getName();
if (name.equals("account_id")) {
Object values = synFields1.getValue();
try {
if (values == JSONObject.NULL) {
// Handle NULL
} else if (values instanceof JSONObject) {
JSONObject jsonObject1 = null;
try {
jsonObject1 = new
JSONObject(String.valueOf(synFields1.getValue()));
String value = ((JSONObject) values).getString("value");
String labels = ((JSONObject) values).getString("label");
account_names.add(labels);
} catch (JSONException e) {
e.printStackTrace();
}
} else {
String value_accounts = String.valueOf(synFields1.getValue());
account_names.add(value_accounts);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
SynField.java:
public class SynFields {
@SerializedName("name")
@Expose
private String name;
@SerializedName("value")
@Expose
private Object value;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
@SerializedName("label")
@Expose
private String label;
}
您可以使用 Gson
将 Object
转换为 JSONObject
。
这样试试
try {
if (values == Null) {
// Handle null object
continue;
}
if(values instanceof String){
String value_accounts = String.valueOf(values);
account_names.add(value_accounts);
}else{
Gson gson = new Gson();
JSONObject jsonObject = new JSONObject(gson.toJson(values)); // convert Object to JSONObject
String value = jsonObject.getString("value");
String labels = jsonObject.getString("label");
account_names.add(labels);
}
} catch (JSONException e) {
e.printStackTrace();
}