0

我有一个 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);

                            }
                        });
4

4 回答 4

0

像这样使用:

String value = jsonCities.getString(Integer.toString(i));
于 2012-09-04T14:46:01.733 回答
0

您可以使用 JsonObject 的 has() 方法找到密钥是否可用,如果找到密钥则返回 true,否则返回 false。

if(jsonCities.has(""+i)){
   // do the thing
}

从此链接检查更多方法

http://www.json.org/javadoc/org/json/JSONObject.html

于 2012-09-04T14:50:40.330 回答
0

跳过6作为

    for(int i=1;i<=len;i++)
    {    
          if(i == 6 )
          {
               i += 1;
          }
          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);
          }
          ...
          ...
于 2012-09-04T14:57:59.253 回答
0

您可以更改此“for”循环

for(int i=1;i<=len;i++)

Iterator it = jsonCities.keys();
while(it.hasNext())
{
    String key = it.next().toString();
    ...
}
于 2012-09-04T14:58:02.020 回答