我有一个 JSONObject 并具有如下值:
{
"Success": true,
"City": {
"1": "noida",
"2": "Delhi",
"3": "Kanpur",
"4": "Gurgaon",
"5": "Mumbai",
"7": "Noida",
"8": "NEWYORK",
"9": "Patna"
}
}
我对每个值都有一个键值,但键“6”没有值。所以它返回“No value for 6”异常。我试图通过使用此代码来删除它,但失败了......请记住,城市将以字符串 [] 的形式在微调器中呈现。
代码如下:
JSONObject jsonCities = jsonObject.getJSONObject("City");
int len = jsonCities.length();
Log.i("Length of jsonCities is :", ""+len);
for(int i=1;i<=len;i++)
{
if(jsonCities.get(""+i)==null)
{
Log.i("NULL VALUE", "NULL VALUE");
continue;
}
else
{
Log.i("Cities :",""+jsonCities.getString(""+i));
cities_hashmap.put(""+i, ""+jsonCities.getString(""+i));
Log.i("cities_hashmap", ""+cities_hashmap);
}
}
cities = new String[cities_hashmap.size()];
for(int j=1;j<=cities_hashmap.size();j++)
{
Log.i("CITY IS :", cities_hashmap.get(""+j));
cities[j-1] = cities_hashmap.get(""+j);
Log.i("City Array in String form is :", ""+cities);
}
context.runOnUiThread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Registration.this, android.R.layout.simple_spinner_item, cities);
country.setAdapter(adapter);
}
});