【问题标题】:How to get the inner array from json in android?如何从android中的json获取内部数组?
【发布时间】:2017-07-04 05:06:22
【问题描述】:
{"Suggestions":[{"Itinerary_1":[{"Cities":"Madurai - Pazhamudir Cholai (Madurai) - Pillayarpatti - Chennai","Citieids":"14-85-114-2","Kms":595}],"Itinerary_2":[{"Cities":"Madurai - Pillayarpatti - Pillayarpatti - Chennai","Citieids":"14-114-114-2","Kms":560}],"Itinerary_3":[{"Cities":"Madurai - Pillayarpatti - Pillayarpatti - Chennai","Citieids":"14-114-114-2","Kms":560}]}]}

这是我的 json 响应。这个 json 有一个内部数组我如何获取值

这是我的代码:

class itinerarydetails extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         */
        boolean failure = false;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            // Check for success tag
            int success;


            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("orgin", "14"));
            params.add(new BasicNameValuePair("destination", "2"));
            params.add(new BasicNameValuePair("orgin_date", "28/06/2017"));
            params.add(new BasicNameValuePair("destination_date", "07/07/2017"));

             json = jsonParser.makeHttpRequest(itineraryurl, "GET",
                    params);
            try {

                //Suggestions
                jsonarray = json.getJSONArray("Suggestions");
               // JSONArray list = mainObj.getJSONArray("prodCat_list");

                String check;
                if(jsonarray != null) {
                   // Toast.makeText(getApplicationCon                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          text(), "MAIN ARRAY IS NOT EMPTY ITS HAVE A VALUE",
                    //  Toast.LENGTH_SHORT).show();

                    Log.d("check","MAIN ARRAY IS NOT EMPTY ITS HAVE A VALUE");

                    for (int i = 0; i < jsonarray.length(); i++) {
                        //   JSONObject elem = list.getJSONObject(i);

                        JSONObject object = jsonarray.getJSONObject(i);

                        //JSONArray prods = elem.getJSONArray("prods");

                        jsoninsidearray = object.getJSONArray("Itinerary_1");

                        if(jsoninsidearray != null) {

                            Log.d("check","INSIDE ARRAY IS NOT EMPTY ITS HAVE A VALUE");

                            for (int j = 0; j < jsoninsidearray.length(); j++) {
                                JSONObject innerElem = jsoninsidearray.getJSONObject(j);


                                if(innerElem != null) {


                                    Log.d("check","innerElement ARRAY IS NOT EMPTY ITS HAVE A VALUE");
                                    cityjson = innerElem.getString("Cities");
                                    itinerary = innerElem.getString("Citieids");
                                    totalkms = innerElem.getString("Kms");

                                }
                               else
                                {
                                    Log.d("check","innerElement ARRAY IS EMPTY");

                                }

                            }
                        }
                        else
                        {
                           Toast.makeText(getApplicationContext(), "INSIDE ARRAY IS EMPTY",
                                   Toast.LENGTH_SHORT).show();

                            Log.d("check","INSIDE ARRAY IS EMPTY");
                        }


                    }
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "MAIN ARRAY IS EMPTY",
                            Toast.LENGTH_SHORT).show();

                    Log.d("check","MAIN ARRAY IS EMPTY");
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }


        protected void onPostExecute(String file_url) {

            Toast.makeText(getApplicationContext(), "hai",
                    Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), cityjson, Toast.LENGTH_SHORT).show();



        }

    }

我是 Vignesh,我有一个疑问,所以请在下面提供帮助 但是显示了空值。如何从 json 中获取内部数组中的值。

【问题讨论】:

  • 我认为这回答了你的问题:stackoverflow.com/questions/17673057/…
  • 在此,有一个建议数组,其中有三个不同的 Array("Itinerary_1","Itinerary_2","Itinerary_3") 。但是你给出的答案有 (prodCat_list) 数组和它有一个 Multiple 数组,但所有内容都具有相同的名称,因此可以轻松调用。我也尝试过这个想法,但它显示的是空值。
  • object.getJSONArray("Itinerary_"+ (i+1)) 是完美的,因为没有“Itinerary_0”。比它我会停止解析亲爱的。检查我的答案

标签: android


【解决方案1】:

让我们尝试了解您的输入 JSON 对象的结构。

  • 建议 (JSONArray)
    • 第一个对象 (JSONObject)
      • Itinerary_1 (JSONArray)
        • 第一个对象 (JSONObject)
          • 城市(字符串)
          • citieids(字符串)
          • 公里(字符串)
      • Itinerary_2 (JSONArray)
        • 第一个对象 (JSONObject)
          • 城市(字符串)
          • citieids(字符串)
          • 公里(字符串)
      • Itinerary_3 (JSONArray)
        • 第一个对象 (JSONObject)
          • 城市(字符串)
          • citieids(字符串)
          • 公里(字符串)
    • 第二个对象 (JSONObject)
      • Itinerary_1 (JSONArray)
        • 第一个对象 (JSONObject)
          • 城市(字符串)
          • citieids(字符串)
          • 公里(字符串)
      • Itinerary_2 (JSONArray)
        • 第一个对象 (JSONObject)
          • 城市(字符串)
          • citieids(字符串)
          • 公里(字符串)
      • Itinerary_3 (JSONArray)
        • 第一个对象 (JSONObject)
          • 城市(字符串)
          • citieids(字符串)
          • 公里(字符串)
    • 等等....

格式化的 JSON 对象

{
   "Suggestions":[
      {
         "Itinerary_1":[
            {
               "Cities":"Madurai - Pazhamudir Cholai (Madurai) - Pillayarpatti - Chennai",
               "Citieids":"14-85-114-2",
               "Kms":595
            }
         ],
         "Itinerary_2":[
            {
               "Cities":"Madurai - Pillayarpatti - Pillayarpatti - Chennai",
               "Citieids":"14-114-114-2",
               "Kms":560
            }
         ],
         "Itinerary_3":[
            {
               "Cities":"Madurai - Pillayarpatti - Pillayarpatti - Chennai",
               "Citieids":"14-114-114-2",
               "Kms":560
            }
         ]
      }
   ]
}

因此,您的代码应该有一个额外的 for 循环来循环 Suggestions 中每个 JSONObject 的 3 个行程元素。

这是更新后的代码,请查看新的 for 循环,其中添加了 index 作为迭代计数器。

    try {
        JSONObject json = new JSONObject("{\"Suggestions\":[{\"Itinerary_1\":[{\"Cities\":\"Madurai - Pazhamudir Cholai (Madurai) - Pillayarpatti - Chennai\",\"Citieids\":\"14-85-114-2\",\"Kms\":595}],\"Itinerary_2\":[{\"Cities\":\"Madurai - Pillayarpatti - Pillayarpatti - Chennai\",\"Citieids\":\"14-114-114-2\",\"Kms\":560}],\"Itinerary_3\":[{\"Cities\":\"Madurai - Pillayarpatti - Pillayarpatti - Chennai\",\"Citieids\":\"14-114-114-2\",\"Kms\":560}]}]}");
        JSONArray jsonarray = json.getJSONArray("Suggestions");
        if(jsonarray != null) {
            Log.d("check","MAIN ARRAY IS NOT EMPTY ITS HAVE A VALUE");
            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject object = jsonarray.getJSONObject(i);
                // ** START NEW FOR LOOP
                for (int index=1; index < 4; index++) {
                    Log.d("check","-------------------------");
                    Log.d("check","Print Itinerary_" + (index));
                    JSONArray jsoninsidearray = object.getJSONArray("Itinerary_" + index);
                    if(jsoninsidearray != null) {
                        Log.d("check","INSIDE ARRAY IS NOT EMPTY ITS HAVE A VALUE");
                        for (int j = 0; j < jsoninsidearray.length(); j++) {
                            JSONObject innerElem = jsoninsidearray.getJSONObject(j);
                            if(innerElem != null) {
                                Log.d("check","innerElement ARRAY IS NOT EMPTY ITS HAVE A VALUE");
                                String cityjson = innerElem.getString("Cities");
                                String itinerary = innerElem.getString("Citieids");
                                String totalkms = innerElem.getString("Kms");
                                Log.d("check", cityjson + " | " + itinerary + " | " + totalkms);
                            } else {
                                Log.d("check","innerElement ARRAY IS EMPTY");
                            }
                        }
                    } else {
                        Log.d("check","INSIDE ARRAY IS EMPTY");
                    }
                } // ** END NEW FOR LOOP
            Log.d("check", "");
            }
        } else {
            Log.d("check","MAIN ARRAY IS EMPTY");
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

样品运行

07-03 22:03:05.028 30616-30616/a D/check: MAIN ARRAY IS NOT EMPTY ITS HAVE A VALUE
07-03 22:03:05.028 30616-30616/a D/check: --------------------------
07-03 22:03:05.028 30616-30616/a D/check: Print Itinerary_1
07-03 22:03:05.028 30616-30616/a D/check: INSIDE ARRAY IS NOT EMPTY ITS HAVE A VALUE
07-03 22:03:05.028 30616-30616/a D/check: innerElement ARRAY IS NOT EMPTY ITS HAVE A VALUE
07-03 22:03:05.028 30616-30616/a D/check: Madurai - Pazhamudir Cholai (Madurai) - Pillayarpatti - Chennai | 14-85-114-2 | 595
07-03 22:03:05.028 30616-30616/a D/check: --------------------------
07-03 22:03:05.028 30616-30616/a D/check: Print Itinerary_2
07-03 22:03:05.028 30616-30616/a D/check: INSIDE ARRAY IS NOT EMPTY ITS HAVE A VALUE
07-03 22:03:05.028 30616-30616/a D/check: innerElement ARRAY IS NOT EMPTY ITS HAVE A VALUE
07-03 22:03:05.029 30616-30616/a D/check: Madurai - Pillayarpatti - Pillayarpatti - Chennai | 14-114-114-2 | 560
07-03 22:03:05.029 30616-30616/a D/check: --------------------------
07-03 22:03:05.029 30616-30616/a D/check: Print Itinerary_3
07-03 22:03:05.029 30616-30616/a D/check: INSIDE ARRAY IS NOT EMPTY ITS HAVE A VALUE
07-03 22:03:05.029 30616-30616/a D/check: innerElement ARRAY IS NOT EMPTY ITS HAVE A VALUE
07-03 22:03:05.029 30616-30616/a D/check: Madurai - Pillayarpatti - Pillayarpatti - Chennai | 14-114-114-2 | 560

【讨论】:

  • 感谢您的解决方案。此代码完美运行,先生。但仅输出显示在静态数据中。不从 json 中检索数据(动态)。我认为问题是 JSON ARRAY 没有转换成 JSON OBJECT
  • 我没有得到你的cmets,如果你能详细说明我可以帮忙吗?
【解决方案2】:

只需更改此行jsoninsidearray = object.getJSONArray("Itinerary_1");

jsoninsidearray = object.getJSONArray("Itinerary_"+i);

【讨论】:

  • 好的试试这个,如果您有任何疑问,请告诉我
  • 感谢您的回复。我试过了,但只显示空值。
  • 只是为了我以后的理解,第二个代码和第一个有什么不同?
  • for (int i = 0; i
  • 有任何疑问请咨询我。
【解决方案3】:

这里你得先创建jsonobject试试这个

JSONObject object = new JSONObject(json);  
JSONArray data = object.getJSONArray("Suggestions");   

        JSONObject obj1 = data.getJSONObject(0);

            JSONArray inner = obj1.getJSONArray("Itinerary_1");
            String Cities=inner.getString(0);
            String Citiesids=inner.getString(1);
            String Kms=inner.getString(2);
//and so on copy and change the name of Itinerary_*

}

【讨论】:

    【解决方案4】:

    希望对你有帮助

    JSONObject mainObj = = new JSONObject(response);
    
    JSONArray Suggestionslist = mainObj.getJSONArray("Suggestions");
        if(Suggestionslist != null){
    
        for(int i = 0; i < Suggestionslist.length();i++){
                 JSONObject elem = list.getJSONObject(i);
                if(elem != null){
                     JSONArray Itinerary = elem.getJSONArray("Itinerary_"+ (i+1));
                    if(Itinerary != null){ 
    
                           for(int j = 0; j < Itinerary.length();j++){
                            JSONObject innerElem = Itinerary.getJSONObject(j);
                            if(innerElem != null){
                                String Cities = innerELem.getString("Cities");
                                String Citieids = innerElem.getString("Citieids");
                                String Kms = innerElem.getString("Kms");
                            }
                        }  
    
                     }
        }
    
    }
    

    【讨论】:

    • 如有问题请提出
    • 什么是结果? “Itinerary_”+ (i+1) 是完美的,因为没有“Itinerary_0”。比它我会停止解析亲爱的。
    • 感谢您的回复。我试过了,但只显示空值
    【解决方案5】:

    这是 innerArray 的完整解决方案

          if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);
    
    
    
                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("Suggestions");
    
                System.out.println("====================///////////" + jarray);
                if (jarray != null) {
                    for (int i = 0; i < jarray.length(); i++) {
                        JSONObject object = jarray.getJSONObject(i);
    
    
                        System.out.print("obj//////////" + object.length() + "\n");
                        int m;
    
    
                        for (int i1 = 1; i1 <= object.length(); i1++) {
    
                            System.out.println("inner======  inside second for loop  =====");
    
                            JSONArray jarray1 = object.getJSONArray("Itinerary_" + i1);
    
                            JSONObject object1 = jarray1.getJSONObject(i);
    
                            //JSONObject jsoninside = object.getJSONObject("Itinerary_"+ i1);
    
                            System.out.println("inner====================///////////\\\\" + object1);
    
                            if (object1 != null) {
                                citynames = object1.getString("Cities");
                                citykms = object1.getString("Kms");
    
                            } else {
                                Log.d("check", "INSIDE ARRAY IS EMPTY");
                            }
    
    
                        }
    
                        return true;
                    }
    
                }
            } else {
                Log.d("check", "MAIN ARRAY IS EMPTY");
            }
    

    【讨论】:

    • 这段代码完美运行(任何疑问请添加评论)。我会解释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 2022-01-20
    • 1970-01-01
    • 2020-08-18
    • 2021-11-04
    • 2014-10-08
    相关资源
    最近更新 更多