在android中的一个对象下解析多个数组json

时间:2014-08-25 11:58:33

标签: android json parsing

我如何解析这些数据:

{  
   "data":{  
      "ambulance":[  
         {  
            "ID":"1",
            "Title":"title1",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"2",
            "Title":"title1",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"3",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"4",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"5",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"6",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"7",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"8",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"9",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"10",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"11",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"12",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         }
      ],
      "bloodbank":[  
         {  
            "ID":"1",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"2",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"3",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         }
      ],
      "hospital":[  
         {  
            "ID":"1",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"2",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"3",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"4",
            "Title":"Charpara",
            "Longitude":"45.156849",
            "Latitude":"27.158945",
            "Address":"Mymensingh Medical College"
         },
         {  
            "ID":"5",
            "Title":"Sylhet Osmani Medical College",
            "Longitude":"78.1245887",
            "Latitude":"76.054846",
            "Address":"Osmani Meical College road, Sylher"
         }
      ]
   }
}

5 个答案:

答案 0 :(得分:0)

说"数据"是你的JSON字符串的名称,然后这样做(不要忘记捕获JSONException):

JSONObject dataJson = new JSONObject(data).getJSONObject("data");
JSONArray ambulance = dataJson.getJSONArray("ambulance");
JSONArray bloodbank = dataJson.getJSONArray("bloodbank");
JSONArray hospital = dataJson.getJSONArray("hospital");

例如,您可以通过迭代JSONArrays并调用ambulance.getJSONObject(i)来访问特定对象。然后你只需要调用getString(" ID")等等来获取值。

答案 1 :(得分:0)

您需要按照json保持正确的数据结构。

在你的情况下:

public class Response{
    public List<Ambulance> aList = new ArrayList<Ambulance>();
    public List<BloodBank> bList = new ArrayList<Ambulance>();

    //getter setter for both lists
}

public class Ambulance{
    private String  ID = "";
    private String  Title = "";
    private Double Longitude=0.0;
    private Double Latitude=0.0;
    private String  Address = "";
    public String getID() {
        return ID;
    }
    public void setID(String iD) {
        ID = iD;
    }
    public String getTitle() {
        return Title;
    }
    public void setTitle(String title) {
        Title = title;
    }
    public Double getLongitude() {
        return Longitude;
    }
    public void setLongitude(Double longitude) {
        Longitude = longitude;
    }
    public Double getLatitude() {
        return Latitude;
    }
    public void setLatitude(Double latitude) {
        Latitude = latitude;
    }
    public String getAddress() {
        return Address;
    }
    public void setAddress(String address) {
        Address = address;
    }

}

public class BloodBank{
    private String  ID = "";
    private String  Title = "";
    private Double Longitude=0.0;
    private Double Latitude=0.0;
    private String  Address = "";
    public String getID() {
        return ID;
    }
    public void setID(String iD) {
        ID = iD;
    }
    public String getTitle() {
        return Title;
    }
    public void setTitle(String title) {
        Title = title;
    }
    public Double getLongitude() {
        return Longitude;
    }
    public void setLongitude(Double longitude) {
        Longitude = longitude;
    }
    public Double getLatitude() {
        return Latitude;
    }
    public void setLatitude(Double latitude) {
        Latitude = latitude;
    }
    public String getAddress() {
        return Address;
    }
    public void setAddress(String address) {
        Address = address;
    }

}

然后解析为:

Response response = new Gson().fromJson(responseString,
                    Response.class);

答案 2 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

       try{
            String jsonResponse = "{\"data\":{\"ambulance\":[{\"ID\":\"1\",\"Title\":\"title1\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"2\",\"Title\":\"title1\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"3\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"4\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"5\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"6\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"7\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"8\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"9\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"10\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"11\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"12\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"}],\"bloodbank\":[{\"ID\":\"1\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"2\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"3\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"}],\"hospital\":[{\"ID\":\"1\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"2\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"3\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"4\",\"Title\":\"Charpara\",\"Longitude\":\"45.156849\",\"Latitude\":\"27.158945\",\"Address\":\"Mymensingh Medical College\"},{\"ID\":\"5\",\"Title\":\"Sylhet Osmani Medical College\",\"Longitude\":\"78.1245887\",\"Latitude\":\"76.054846\",\"Address\":\"Osmani Meical College road, Sylher\"}]}}";

             JSONObject responeJson = new JSONObject(jsonResponse);
             JSONObject dataJson = responeJson.optJSONObject("data");

            ArrayList<HashMap<String,String>> ambulanceList = new ArrayList<HashMap<String, String>>();
            JSONArray ambulanceJsonArray = dataJson.getJSONArray("ambulance");
            for(int i=0;i<ambulanceJsonArray.length();i++){
                HashMap<String,String> ambulanceMap = new HashMap<String, String>();
                ambulanceMap.put("ID", ambulanceJsonArray.getJSONObject(i).getString("ID"));
                ambulanceMap.put("Title", ambulanceJsonArray.getJSONObject(i).getString("Title"));
                ambulanceMap.put("Longitude", ambulanceJsonArray.getJSONObject(i).getString("Longitude"));
                ambulanceMap.put("Latitude", ambulanceJsonArray.getJSONObject(i).getString("Latitude"));
                ambulanceMap.put("Address", ambulanceJsonArray.getJSONObject(i).getString("Address"));
                ambulanceList.add(ambulanceMap);
            }

            ArrayList<HashMap<String,String>> hospitalList = new ArrayList<HashMap<String, String>>();
            JSONArray hospitalJsonArray = dataJson.getJSONArray("hospital");
            for(int i=0;i<hospitalJsonArray.length();i++){
                HashMap<String,String> hospitalMap = new HashMap<String, String>();
                hospitalMap.put("ID", hospitalJsonArray.getJSONObject(i).getString("ID"));
                hospitalMap.put("Title", hospitalJsonArray.getJSONObject(i).getString("Title"));
                hospitalMap.put("Longitude", hospitalJsonArray.getJSONObject(i).getString("Longitude"));
                hospitalMap.put("Latitude", hospitalJsonArray.getJSONObject(i).getString("Latitude"));
                hospitalMap.put("Address", hospitalJsonArray.getJSONObject(i).getString("Address"));
                hospitalList.add(hospitalMap);
            }

            ArrayList<HashMap<String,String>> bloodbankList = new ArrayList<HashMap<String, String>>();
            JSONArray bloodbankJsonArray = dataJson.getJSONArray("bloodbank");
            for(int i=0;i<bloodbankJsonArray.length();i++){
                HashMap<String,String> bloodbankMap = new HashMap<String, String>();
                bloodbankMap.put("ID", bloodbankJsonArray.getJSONObject(i).getString("ID"));
                bloodbankMap.put("Title", bloodbankJsonArray.getJSONObject(i).getString("Title"));
                bloodbankMap.put("Longitude", bloodbankJsonArray.getJSONObject(i).getString("Longitude"));
                bloodbankMap.put("Latitude", bloodbankJsonArray.getJSONObject(i).getString("Latitude"));
                bloodbankMap.put("Address", bloodbankJsonArray.getJSONObject(i).getString("Address"));
                bloodbankList.add(bloodbankMap);
            }


            for (int i=0;i<ambulanceList.size();i++) {
                HashMap<String, String> ambulanceMapData = ambulanceList.get(i);
                System.out.print("ID :" + ambulanceMapData.get("ID"));
                System.out.print("Title :" + ambulanceMapData.get("Title"));
                System.out.print("Longitude :" + ambulanceMapData.get("Longitude"));
                System.out.print("Latitude :" + ambulanceMapData.get("Latitude"));
                System.out.print("Address :" + ambulanceMapData.get("Address"));
            }
            for (int i=0;i<bloodbankList.size();i++) {
                HashMap<String, String> bloodbankMapData = bloodbankList.get(i);
                System.out.print("ID :" + bloodbankMapData.get("ID"));
                System.out.print("Title :" + bloodbankMapData.get("Title"));
                System.out.print("Longitude :" + bloodbankMapData.get("Longitude"));
                System.out.print("Latitude :" + bloodbankMapData.get("Latitude"));
                System.out.print("Address :" + bloodbankMapData.get("Address"));
            }
            for (int i=0;i<hospitalList.size();i++) {
                HashMap<String, String> hospitalMapData = hospitalList.get(i);
                System.out.print("ID :" + hospitalMapData.get("ID"));
                System.out.print("Title :" + hospitalMapData.get("Title"));
                System.out.print("Longitude :" + hospitalMapData.get("Longitude"));
                System.out.print("Latitude :" + hospitalMapData.get("Latitude"));
                System.out.print("Address :" + hospitalMapData.get("Address"));
            }

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

答案 3 :(得分:0)

使用 this link doInBackground方法,您必须拥有以下代码:

if (jsonStr != null) {
    try {
        JSONObject jsonObj = new JSONObject(jsonStr);


        JSONObject data = jsonObj.getJSONObject("data");

        // Getting JSON Array node 
        JSONArray ambulance = jsonObj.getJSONArray("ambulance");
        // looping through All ambulance 
        for (int i = 0; i < ambulance.length(); i++) {
            JSONObject c = contacts.getJSONObject(i);

            String id = c.getString("ID");
            String Title = c.getString("Title");
            String Longitude = c.getString("Longitude");
            String Latitude = c.getString("Latitude");
            String Address = c.getString("Address");
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
} else {
    Log.e("ServiceHandler", "Couldn't get any data from the url");
}

答案 4 :(得分:0)

JSONObject dataJsonObject = new JSONObject(toParseData);
                JSONArray ambulanceArray = dataJsonObject.getJSONArray("ambulance");
                for(int i=0;i<ambulanceArray.length();i++){
                    JSONObject ambulanceChildNode = ambulanceArray.getJSONObject(i);
                    String ID=ambulanceChildNode.getString("ID");
                }
                JSONArray bloodBankArray = dataJsonObject.getJSONArray("bloodbank");
                for(int i=0;i<bloodBankArray.length();i++){
                    JSONObject ambulanceChildNode = ambulanceArray.getJSONObject(i);
                    String title=bloodBankArray.getString("Title");
                }

。 。 。 。

.......等等。不要太懒了。尝试后发布问题,如果您有任何问题