Android JSONException“密钥没有值”
我是从这样的网站获取JSON对象:
JSONObject response = new JSONObject(read(placeConnection));
当我执行response.toString();
我得到了该JSON对象的整个字符串以及我想要的键和值。
但是,当我执行response.getString("countryName")
我得到了上面的异常,并且表示没有该键的值。
以字符串形式显示JSON:
{"geonames":[{"countryId":"6252001","countryName":"United States","adminCode1":"NC","fclName":"city, village,...","countryCode":"US","lng":"-76.98661","fcodeName":"populated place","toponymName":"Riverdale","distance":"1.30053","fcl":"P","name":"Riverdale","fcode":"PPL","geonameId":4488145,"lat":"34.99599","adminName1":"North Carolina","population":0}]}
这是错误:
org.json.JSONException: No value for countryName
你的字段在一个数组结构中,所以你的getString在第一级不起作用。
你需要做一些事情(未经测试):
JSONArray geonames = response.getJSONArray("geonames");
for(int i = 0 ; i < geonames.length() ; i++){
JSONObject geo = (JSONObject)geonames.get(i);
String countryName = geo.getString("countryName");
// Do something with it
}
链接地址: http://www.djcxy.com/p/20121.html