【发布时间】:2014-03-15 07:05:02
【问题描述】:
我想从此代码中提取男性对象,以便我的输出显示所有性别 里面有男的。
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "ravi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "Johnny Depp",
"email": "johnny_depp@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
这是我试图获取的 json 代码
`if (jsonStr != null) { 尝试 { JSONObject jsonObj = new JSONObject(jsonStr);
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
if(TAG_GENDER == male ){
String name = c.getString(TAG_GENDER);
HashMap<String, String> contact = new HashMap<String, String>();
contact.put(TAG_GENDER, name);
contactList.add(contact);
}} }
catch (JSONException e) {
e.printStackTrace();
}
}
else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}`
【问题讨论】:
-
你试过的解析代码在哪里?
-
什么是
if(TAG_GENDER == male ){?TAG_GENDER和male是什么? -
私有静态字符串 url = "api.androidhive.info/contacts";这就是我在代码中声明的内容: private static final String TAG_CONTACTS = "contacts";私有静态最终字符串 TAG_NAME = "名称";私有静态最终字符串 TAG_GENDER = "性别";
-
@user3422497
TAG_GENDER是什么?