检查嵌套中是否存在列 JSON
Check if column exists in Nested JSON
下面是我的样本JSON
{
"_id":{
"$oid":"1564t8re13e4ter86"
},
"object":{
"shop":"shop1",
"domain":"Divers",
"sell":[
{
"location":{
"zipCode":"58000",
"city":"NEVERS"
},
"properties":{
"description":"ddddd!!!!",
"id":"f1re67897116fre87"
},
"employee":[
{
"name":"employee1",
"id":"245975"
},
{
"name":"employee2",
"id":"458624"
}
],
"customer":{
"name":"Customer1",
"custid":"test_réf"
}
"preference":"talking"
}
]
}
}
在上面的示例中,我想知道此特定列是否存在于 $.object.sell.preference exists 中,因为在大多数情况下它不存在会导致失败。
此外,如果有任何选项我可以设置为在使用 readcontext.read($...)
时作为 return 为空或空白,我也很好
提前致谢
此致
尝试:
bool(json['object']['sell'][0]["preference"])
如果为空则return为假,反之亦然
注意:我指的是 java
是的,可以使用 jsonObject.has() 方法进行检查。
假设您有响应变量,其中有完整的 JSON
你可以这样进行
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONObject("object").getJSONArray("sell");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
if (jsonObject1.has("preference")) {
// Exist
} else {
//Not Exist
}
}
下面是我的样本JSON
{
"_id":{
"$oid":"1564t8re13e4ter86"
},
"object":{
"shop":"shop1",
"domain":"Divers",
"sell":[
{
"location":{
"zipCode":"58000",
"city":"NEVERS"
},
"properties":{
"description":"ddddd!!!!",
"id":"f1re67897116fre87"
},
"employee":[
{
"name":"employee1",
"id":"245975"
},
{
"name":"employee2",
"id":"458624"
}
],
"customer":{
"name":"Customer1",
"custid":"test_réf"
}
"preference":"talking"
}
]
}
}
在上面的示例中,我想知道此特定列是否存在于 $.object.sell.preference exists 中,因为在大多数情况下它不存在会导致失败。
此外,如果有任何选项我可以设置为在使用 readcontext.read($...)
时作为 return 为空或空白,我也很好提前致谢
此致
尝试:
bool(json['object']['sell'][0]["preference"])
如果为空则return为假,反之亦然
注意:我指的是 java
是的,可以使用 jsonObject.has() 方法进行检查。 假设您有响应变量,其中有完整的 JSON
你可以这样进行
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONObject("object").getJSONArray("sell");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
if (jsonObject1.has("preference")) {
// Exist
} else {
//Not Exist
}
}