我正在关注 android 中的 JSON 教程。我设法将文件中的 JSON 输出到ListView
. 但是,我不太明白如何引用特定项目。这是json文件:
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "[email protected]",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "Figaro",
"email": "[email protected]",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c202",
"name": "Johnny Depp",
"email": "[email protected]",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
}
]
}
第一件事:如何仅输出“性别 = 男性”等项目?第二件事:每当我在我的 .Ravi Tamada 上按下例如时,我ListView
怎样才能让它只加载它的移动设备?换句话说,如何在我按下的项目和应该显示的信息之间设置链接,同时考虑到它与按下的项目的关系?
这就是我从 json 中检索项目的方式:
switch (view.getId()){
case R.id.button1:
loadmyjson();
try {
JSONObject jsonObj = new JSONObject(jsonString);
JSONArray letters = jsonObj.getJSONArray("contacts");
for (int i = 0; i < letters.length(); i++){
JSONObject first = letters.getJSONObject(i);
String gender = first.getString("gender");
Log.e("and that means", " " + gender);
if (first.getString("name") == "Figaro"){
where.add(gender);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("and that means", " " + where);
break;
}
问题是这个 if 语句总是错误的。
而是first.getString("name") == "Figaro"
尝试first.getString("name").equals("Figaro")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句