嘿,我是使用 android 的网络服务新手。我想知道如何检查 JSON 中是否存在某个值? 这是一个示例 JSON
{
"contacts": [
{
"id": "c200",
"name": "Michael Jordan",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92fff8d2f5fff3fbfebcf1fdff" rel="noreferrer noopener nofollow">[email protected]</a>",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
}
]
}
我想知道 JSON 中是否存在 Michael Jordan 这个名字。请帮我解决这个问题,我只知道如何使用 has 检查 JSON 中是否存在该对象,但是如果我想检查该值怎么办?
最佳答案
您需要将内容读入 JSONObject。然后您可以添加此代码 fragment
JSONArray jsonArray = yourJSONObject.getJSONArray("contacts"); //here yourJSONObject is the object which has the array in your question.
for(int i = 0; i<jsonArray.length(); i++) {
JSONObject jObject = jsonArray.getJSONObject(i);
String name = jObject.getString("name");
if(name.equals("Michael Jordan")) {
System.out.println("Name Exists");
}
}
关于android - 如何知道 Android 上的 JSON 中是否存在对象值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17779201/