如何从android中的json数组中获取Json值?

时间:2017-01-30 07:40:05

标签: android arrays json

我需要从Json Array获取我的国家名称但是当我尝试代码时它没有显示任何内容.. 这是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<>();
        }

我知道我错过了一些东西......但没有得到什么?

4 个答案:

答案 0 :(得分: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();
            }

答案 1 :(得分: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();
    }

答案 2 :(得分: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) { }

答案 3 :(得分: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();
        }