2016-12-23 66 views
-1

下面是一個網絡服務的網址:從Web服務獲取數據並將其存儲在數組列表

http://watsmo.com/clients/techmate/getCity.php

它採用以下格式返回JSON數據:

{ 
    "Andhra Pradesh":{ 
     "37":"Anantapur", 
     "48":"Bhimavaram", 
     "183":"Chittoor", 
     "191":"Eluru", 
     "52":"Guntur", 
     "50":"Kakinada", 
     "185":"Karnul", 
     "16":"Rajahmundry", 
     "182":"Tirupati", 
     "17":"Vijaywada", 
     "18":"Visakhapatnam", 
     "201":"Vizianagram" 
    }, 
    "Bihar":{ 
     "88":"Patna" 
    }, 
    "Chhattisgarh":{ 
     "163":"Bhillai", 
     "164":"Bilaspur", 
     "161":"Raipur" 
    }, 
    "Delhi":{ 
     "68":"Delhi", 
     "61":"New delhi" 
    }, 
    "Gujarat":{ 
     "6":"Ahmedabad", 
     "173":"Bhavnagar", 
     "172":"Jamnagar", 
     "136":"Rajkot", 
     "170":"Surat", 
     "8":"Vadodara" 
    } 
} 

這JSON包含印度的州和相應城市的名稱。

我想創建一個兩個spinners的活動,第一個包含狀態的名稱,另一個包含與第一個微調器中選擇的狀態對應的城市名稱。我不可能對此進行硬編碼,而且我無法弄清楚如何使用Web服務來完成此操作。請幫我解決一下這個。謝謝!

+0

你必須使用迭代器取得鑰匙,並讓'鍵 – rookieDeveloper

+0

的arrayList'如何,但你可以取悅代碼,並給 –

+1

是什麼問題呢?你想要有人做這個任務而不是你? –

回答

0

現在讓你的JSONObject做以下看看是否有幫助

keysOfJson.clear(); 
Iterator<String> iterator = jsonObject.keys(); 
while (iterator.hasNext()) { 
    String key = iterator.next(); 
    if (jsonObject.get(key) instanceof JSONObject) { 
     JSONObject object= jsonObject.getJSONObject(key); 
     if (!keysOfJson.contains(key)) { 
      keysOfJson.add(key); 
      } 
     } 
    } 
+0

我無法讓你能請你告訴我java代碼的全部活動 –

+0

你能否給我你的電子郵件地址,因爲我無法在這裏發佈代碼 –

0
HashMap<int,ArrayList<String>> hmData=new HashMap<>(); 
for(int i=0;i<response.length();i++) 
{ 

JSONObject jobj1=response.getJsonObject(i) 
ArrayList<String> alcities=new ArrayList<>(); 
for(int j=0;j<jobj2.length()){ 
JSONObject jobj2=jobj1.getJsonObject(j); 
alcities.add(jobj2.optString(jobj2)); 
} 
hm.put(i,alcities); 
//parse the cities from json object and store all cities. 

} 

音符後創建的ArrayList

ArrayList<String> keysOfJson=new ArrayList(); 

一個全局變量:未執行但是這可以給你一個粗略的理念。

0

可能對你有幫助。得到不同的城市名和國名列表設置成微調

public static Map<String, Object> jsonToMap(JSONObject json) throws JSONException { 
     Map<String, Object> retMap = new HashMap<String, Object>(); 

     if(json != JSONObject.NULL) { 
      retMap = toMap(json); 
     } 
     return retMap; 
    } 

    public static Map<String, Object> toMap(JSONObject object) throws JSONException { 
     Map<String, Object> map = new HashMap<String, Object>(); 

     Iterator<String> keysItr = object.keys(); 
     while(keysItr.hasNext()) { 
      String key = keysItr.next(); 
      Object value = object.get(key); 

      if(value instanceof JSONArray) { 
       value = toList((JSONArray) value); 
      } 

      else if(value instanceof JSONObject) { 
       value = toMap((JSONObject) value); 
      } 
      map.put(key, value); 
     } 
     return map; 
    } 

    public static List<Object> toList(JSONArray array) throws JSONException { 
     List<Object> list = new ArrayList<Object>(); 
     for(int i = 0; i < array.length(); i++) { 
      Object value = array.get(i); 
      if(value instanceof JSONArray) { 
       value = toList((JSONArray) value); 
      } 

      else if(value instanceof JSONObject) { 
       value = toMap((JSONObject) value); 
      } 
      list.add(value); 
     } 
     return list; 
    } 

這種雙方法用於轉換您的JSON響應分爲兩個

  1. 鍵(串)的地圖對象:國家名稱
  2. 值(圖):CityId,CITYNAME

現在你可以分叉成狀態陣列和市陣列(CityId,城市名稱)

private void makeJsonObjectRequest() { 

     showpDialog(); 

     JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, 
       urlJsonObj, null, new Response.Listener<JSONObject>() { 

      @Override 
      public void onResponse(JSONObject response) { 
       Log.d(TAG, response.toString()); 

       try { 
        Map<String, Object> stringObjectMap = jsonToMap(response); 
        ArrayList<String> statesName = new ArrayList<>(); 
        ArrayList<City> finalcityArrayList= new ArrayList<>(); 
        HashMap<String,String> mCityHashMap = new HashMap<>(); 
        ArrayList<City> cityArrayList = new ArrayList<>(); 
        for (String key : stringObjectMap.keySet()) { 
         System.out.println(key); 
         statesName.add(key); 
         mCityHashMap = (HashMap<String,String>) stringObjectMap.get(key); 
         mCityHashMap.keySet().toArray(); 
         mCityHashMap.values().toArray(); 

         cityArrayList= new ArrayList<>(); 
         HashMap<String,String> childCityHashMap = new HashMap<>(); 
         for(String cityNumber: mCityHashMap.keySet()){ 
          // childCityHashMap = (HashMap<String,String>) mCityHashMap.get(cityNumber); 
          City city = new City(); 
          String strname = mCityHashMap.get(cityNumber); 
          Log.e("TAG","" + strname); 
          city.setId(cityNumber); 
          city.setName(mCityHashMap.get(cityNumber)); 
          cityArrayList.add(city); 
         } 
         finalcityArrayList.addAll(cityArrayList); 

        } 

         Log.e("TAG","State Names "+statesName.size()); 
       Log.e("TAG","City Names"+finalcityArrayList.size()); 


       } catch (JSONException e) { 
        e.printStackTrace(); 
        Toast.makeText(getApplicationContext(), 
          "Error: " + e.getMessage(), 
          Toast.LENGTH_LONG).show(); 
       } 
       hidepDialog(); 
      } 
     }, new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       Toast.makeText(getApplicationContext(), 
         error.getMessage(), Toast.LENGTH_SHORT).show(); 
       // hide the progress dialog 
       hidepDialog(); 
      } 
     }); 

     // Adding request to request queue 
     SpinnerApp.getInstance().addToRequestQueue(jsonObjReq); 
    } 
相關問題