2017-01-30 86 views
-3

我需要從JSON數組我的國家的名字,但是當我試圖代碼的不顯示任何.. 這是JSON輸出如何從android json數組中獲取Json值?

{ 
    "status_code": 200, 
    "status": "OK", 
    "status_message": "Success", 
    "country_details": [ 
    { 
     "country_code": "AF", 
     "country_name": "Afghanistan", 
     "country_iso": "AFG", 
     "country_flag": "http://..../img/countryflags/128x128/af.png", 
     "calling_code": "93", 
     "fancier_count": 2 
    }, 

在這裏,我只想country_name但是當我試圖此代碼..它沒有顯示我什麼.. 這裏是我的代碼:

protected Void doInBackground(Void... params) { 
      String result = ""; 
      try { 
       list.add("Select Country"); 
       Thread.sleep(2000); 
       String data = (URLEncoder.encode("dest", "UTF-8") + "=" + URLEncoder.encode("destination", "UTF-8")); 
       URLConnection conn = new URL("http://.../api/getCountries").openConnection(); 
       conn.setDoOutput(true); 
       OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
       wr.write(data); 
       wr.flush(); 
       BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
       String line = rd.readLine(); 
       if (line == null) { 
        wr.close(); 
        rd.close(); 
        //return Boolean.valueOf(true); 
       } else { 
        result += line; 
       } 
      } catch (Exception e2) { 
       e2.printStackTrace(); 
      } 
      //parse json data 
      try { 
       JSONArray jArray = new JSONArray(result); 
       for (int i = 0; i < jArray.length(); i++) { 
        JSONObject jsonObject = jArray.getJSONObject(i); 
        // add interviewee name to arraylist 


        list.add(jsonObject.getString("country_name")); 

       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     protected void onPreExecute() { 
      super.onPreExecute(); 
      progressDialog = ProgressDialog.show(PMRegisterActivity.this, "Please wait...", "Fetching data", true, false); 
      list = new ArrayList<>(); 
     } 

我知道我缺少這個東西......但沒有得到什麼呢?

回答

1

變化JSON解析如下

  try { 
       JSONObject jObj = new JSONObject(result); 
       JSONArray jArray = jObj.getJSONArray("country_details"); 
       for (int i = 0; i < jArray.length(); i++) { 
        JSONObject jsonObject = jArray.getJSONObject(i); 
        // add interviewee name to arraylist 


        list.add(jsonObject.getString("country_name")); 

       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
+0

確定讓我來試試.. –

+0

確定THX其工作 –

+0

@ z.al請接受回答它是否工作。 –

1

//解析JSON數據

  try { 
     JSONObject resultJsonObject=new JSONObject(result); 
     JSONArray country_detailsJsonArray=resultJsonObject.getJSONArray("country_details"); 
     for (int i=0;i<country_detailsJsonArray.length();i++){ 
      JSONObject countryONJ=country_detailsJsonArray.getJSONObject(i); 
      String country_name=countryONJ.getString("country_name"); 
      Log.d("country_name","="+country_name); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
1

嘗試這樣的事情。

try { 
     JSONObject jsonResponse = new JSONObject(result); 
     JSONArray jsArray= jsonResponse.getJSONArray("country_details"); 
     for (int i = 0; i < jsArray.length(); i++) 
     { 
     JSONObject jsonObject = jsArray.getJSONObject(i); 

     list.add(jsonObject.getString("country_name")); 
     } 
    } catch (JSONException e) { } 
1

我假設你result變量將具有上述Json

變化解析JSON的一部分,這

try { 
      JSONObject obj = new JSONObject(result); 
      JSONArray jArray = obj.getJSONArray("country_details") 
      for (int i = 0; i < jArray.length(); i++) { 
       JSONObject jsonObject = jArray.getJSONObject(i); 
       // add interviewee name to arraylist 


       list.add(jsonObject.getString("country_name")); 

      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
+0

thx我已經gt的答案..但upvoted urs也 –