我收到错误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需要获取特定字符串的值帮我谢谢
使用 具有检查重点是存在于JSON。如果该对象具有名称映射,则返回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. }