2 annbnncnnd AnnBnnCnnD 于 2013.07.22 17:12 提问

查询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 是否存在,应该怎么实现?

3个回答

Jave_
Jave_   2013.07.22 17:27

最简单的办法,JSON变为String判断Contains?

loFataMer
loFataMer   2013.07.22 17:31

在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");
 }
}
cj6585256
cj6585256   2013.07.22 21:57

对 Json转化为String

String sJson = json.toString();
boolean isExist = sJson .contans("Michael Jordan");
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!