应该怎么查询Android Json中已经存在的值?
{
"contacts": [
{
"id": "c200",
"name": "Michael Jordan",
"email": "mj@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
}
]
}
比如我要查询 Michael Jordan 是否存在,应该怎么实现?
最简单的办法,JSON变为String判断Contains?
在JSONObject读取内容,添加:
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");
}
}
对 Json转化为String
String sJson = json.toString();
boolean isExist = sJson .contans("Michael Jordan");