我收到错误org.json.JSONException:PROJECT_NAME没有值这是我的json
{"PROJECTS":[
{
"PROJECT_NUMBER": "2062",
"PROJECT_NAME": "OPW 51183"
},
{
"PROJECT_NUMBER": "404",
"PROJECT_NAME": "404"
},
{
"PROJECT_NUMBER": "2125",
"PROJECT_NAME": "OPW 50016"
},
{
"PROJECT_NUMBER": ""
},
{
"PROJECT_NUMBER": "2130",
"PROJECT_NAME": "OPW 51151 63rd & Shirley SEW S"
},
{
"PROJECT_NUMBER": "2159",
"PROJECT_NAME": "OPW 51226"
}
]
}
和鳕鱼是:
for (int i = 0; i < innerProjectarray.length(); i++)
{
JSONObject obj=innerProjectarray.getJSONObject(i);
String projectnumber1=obj.getString("PROJECT_NUMBER");
String projectname1=obj.getString("PROJECT_NAME");
}
有没有办法找到密钥,如果密钥不存在,那么np需要获取值od特定字符串帮助我谢谢
解决方法:
使用has检查Json中是否存在密钥.如果此对象具有name的映射,则返回true.
喜欢
for (int i = 0; i < innerProjectarray.length(); i++) {
JSONObject obj = innerProjectarray.getJSONObject(i);
if (obj.has("PROJECT_NUMBER")) {
String projectnumber1 = obj.getString("PROJECT_NUMBER");
}
if (obj.has("PROJECT_NAME")) {
String projectname1 = obj.getString("PROJECT_NAME");
}
}
另一种方法是使用optString,如果存在,则返回按名称映射的值,必要时强制它.如果不存在此类映射,则返回空字符串.
for (int i = 0; i < innerProjectarray.length(); i++) {
JSONObject obj = innerProjectarray.getJSONObject(i);
String projNum = obj.optString("PROJECT_NUMBER");
String projName = obj.optString("PROJECT_NAME");
// and use both values.
}