如何使用多个数组

时间:2016-04-29 08:00:30

标签: android json

这是我的JSON字符串。

    { 
      "server_response":
        { 
             "source_response" : 
                       [
                        {"stoppage_name":"sealdah","bus_no":"43@230@234/1@30_A"}
                       ] ,
             "destination_response" :                 
                      [
                        {"stoppage_name":"howrah","bus_no":"43@234/1@30_A"}
                      ]
          }
 }

我认为会有一个' ['在" server_response"之后,但不确定。 我试图检索数据,但代码不起作用。

 try {
            jsonObject = new JSONObject(json_string);
            jsonArray = jsonObject.getJSONArray("server_response");
            int count=0 ;
            String stoppage,busno;
            while(count<1)
            {
                JSONArray JA = jsonArray.getJSONArray(0);
                JSONObject JO = JA.getJSONObject(count);

                stoppage = JO.getString("stoppage_name");
                busno = JO.getString("bus_no");

                Toast.makeText(getApplicationContext(),"Stoppage ="+ stoppage+" Bus no =" +busno, Toast.LENGTH_LONG).show();

                count ++;
            }

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

我犯错的地方。我是JSON和Android的新手。

5 个答案:

答案 0 :(得分:0)

server_response不是JSONArray,而是JSONObject。因为数组有数字键,而不是字符串。

答案 1 :(得分:0)

我已经回答了这类问题..你要做什么..使用多个for循环来获取数组中数组的值。

这是srceen镜头 enter image description here

            JSONArray objJson = new JSONArray(strJSONData);



                 System.out.println("AppUserLogin:"+objJson);
                // Parsing json


                if(arrJson.length()>0)
                {
                for (int i = 0; i < objJson.length(); i++) {
                    try {
                        JSONObject objprod = arrJson.getJSONObject(i);

                        HashMap<String, String> MaplistTemp = new HashMap<String, String>();
                        MaplistTemp.put("replyCode",
                                objprod.getString("replyCode"));

                        MaplistTemp.put("replyCode",
                                objprod.getString("replyCode"));

                        JSONArray objproduct_var = new JSONArray(objprod.getString("LearningStandards"));

                        if ((objproduct_var.length()) > 0) {
                            for (int k = 0; k < objproduct_var.length(); k++) {
                                JSONObject objprodvar = objproduct_var
                                        .getJSONObject(k);



                                    MaplistTemp
                                            .put("1",
                                                    objprodvar
                                                            .getString("1"));
                                    MaplistTemp
                                            .put("2",
                                                    objprodvar
                                                            .getString("2"));
                                    MaplistTemp
                                            .put("3",
                                                    objprodvar
                                                            .getString("3"));
                                    MaplistTemp
                                    .put("4",
                                            objprodvar
                                                    .getString("4"));

                            }
                        }
                        // sub_categorys_details.add(sub_cat_det);

                medpostList.add(MaplistTemp);// adding to final hashmap
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }

答案 2 :(得分:0)

试试这个, {} = object [] = array

try{
jsonObject = new JSONObject(json_string);
injsonObject = jsonObject.getJSONObject("server_response");

jsonArray = injsonObject.getJSONArray("source_reponse");
}catch(Exception e){}

答案 3 :(得分:0)

sonArray = jsonObject.getJSONArray("server_response");

在这里,您尝试将server_response作为数组进行访问。它不是一个数组。 “server_response”是嵌套对象的关键:

{ 
     "source_response" : 
               [
                {"stoppage_name":"sealdah","bus_no":"43@230@234/1@30_A"}
               ] ,
     "destination_response" :                 
              [
                {"stoppage_name":"howrah","bus_no":"43@234/1@30_A"}
              ]
  }

请参阅?这不是一个数组,是一个带有两个键的JSON对象,每个键都有一个数组作为值。

我对android或java不是很熟悉,但在普通的旧javascript中你可以尝试类似的东西:

var sourceResp = jsonObject.server_response.source_response;

var sourceResp = jsonObject["server_response"]["source_response"];

这将生成一个包含一个项目的数组。 我希望这可以帮助你。

答案 4 :(得分:0)

jsonObject = new JSONObject(json_string); jsonServerObject = jsonObject.getJSONObject("server_response"); jsonSourceArray = jsonServerObject.getJSONArray("source_response"); jsonDestinationArray = jsonServerObject.getJSONArray("destination_response"); //Iterate your 2 arrays