问题描述
- 查询Android中存在的值
-
应该怎么查询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");
时间: 2024-08-01 00:36:51