2016-06-08 68 views
-2

我將獲取JSONObject的值並將其放入String Array或ArrayList中。問題是我有一個JSONObject而不是JSONArray,並且我無法更改Webservice。 這是我的JSONObject:如何將JSONObject轉換爲字符串數組或ArrayList <String> android

{ 「THR」: 「تهران」, 「MHD」: 「مشهد」, 「KIH」: 「کیش」, 「IFN」: 「اصفهان」, 「SYZ」 : 「شیراز」, 「TBZ」: 「تبریز」, 「ABD」: 「آبادان」}

這是我的代碼:

ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressbar); 
    RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); 

    progressbar.setVisibility(View.VISIBLE); 
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      Toast.makeText(getApplicationContext(), "Response", Toast.LENGTH_SHORT).show(); 
      progressbar.setVisibility(View.GONE); 

      try { 
       JSONArray array = new JSONArray(response); 
       Toast.makeText(getApplicationContext(), "Try", Toast.LENGTH_LONG).show(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
       Toast.makeText(getApplicationContext(), "Catch", Toast.LENGTH_LONG).show(); 
      } 

     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show(); 
      progressbar.setVisibility(View.GONE); 
     } 
    }); 

    queue.add(stringRequest); 

當我調試我的代碼,它會趕上! 不管怎樣,它都不會去TRY!

我想是這樣的:

String[] strArray = {"تهران","مشهد","کیش","اصفهان","شیراز","تبریز","آبادان"}; 

我怎樣才能得到它呢?

+0

對象無法投射到陣列 –

+1

你可以試試這個? JSONArray array = new JSONArray(「[」+ response +「]); – PankajAndroid

回答

0

添加代碼的修改

ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressbar); 
    RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); 
    ArrayList<String> answersStringArray= new ArrayList<String>(); // define string array 

progressbar.setVisibility(View.VISIBLE); 
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      Toast.makeText(getApplicationContext(), "Response", Toast.LENGTH_SHORT).show(); 
      progressbar.setVisibility(View.GONE); 

      try { 
       JJSONObject jsonObject = new JSONObject(response); //cast responce to jsonObject 
       Iterator<?> keys = jsonObject.keys(); // get the keys of the jsonObject 
       while(keys.hasNext()) {//iterrate over them 
        String key = (String)keys.next(); 
        answersStringArray.add(jsonObject.optString(key));}// add the string to our array of stings 


      } catch (JSONException e) { 
       e.printStackTrace(); 
       Toast.makeText(getApplicationContext(), "Catch", Toast.LENGTH_LONG).show(); 
      } 

     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show(); 
      progressbar.setVisibility(View.GONE); 
     } 
    }); 

    queue.add(stringRequest); 
+1

是的,就是這樣!!!謝謝。 –

0
public static void main(String[] args) { 
//building your jsonObject(took only the 1st four ;P) 
     JSONObject objJsonObject = new JSONObject(); 
     objJsonObject.put("THR", "تهران"); 
     objJsonObject.put("MHD", "مشهد"); 
     objJsonObject.put("KIH", "کیش"); 
     objJsonObject.put("IFN", "اصفهان"); 
     String [] strArray= new String[objJsonObject.length()]; 
     int i = 0; 
//adding your required data into a array 
     for (String key: objJsonObject.keySet()) { 
      strArray[i] = objJsonObject.getString(key); 
      i++; 
     } 
//printing your data 
     for (int j = 0; j < strArray.length; j++) { 
      System.out.println(strArray[j]); 
     } 
} 

希望這有助於....

+0

哦,不,我的JSONObject的長度太長了,這只是我的JSONObject的一部分,我這樣做是爲了簡化問題。 –

+0

@AlirezaNoorali i沒有看到你的jsonObject的長度在這裏有多重要嗎?你可以讓自己更清楚嗎?就你的問題而言,上面的代碼給你你需要的東西... – Abhishek

相關問題