-1
{
  "Catagories": [
    {
      "cat_id": "01",
      "name": "Business",
      "logo": "catagoryLogo1",
      "sub_catagories": [
        {
          "subcat_id": "11",
          "name": "Business11",
          "logo": "logo11",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "user@pidex.ca",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },
        {
          "subcat_id": "12",
          "name": "Business12",
          "logo": "logo12",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "user@pidex.ca",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },
        {
          "subcat_id": "13",
          "name": "Business13",
          "logo": "logo13",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "user@pidex.ca",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },

      ]
    },
    {
      "cat_id": "02",
      "name": "Restorant",
      "logo": "catagoryLogo2",
      "sub_catagories": [
        {
          "subcat_id": "21",
          "name": "Restorant21",
          "logo": "logo21",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "user@pidex.ca",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },
        {
          "subcat_id": "22",
          "name": "Restorant22",
          "logo": "logo22",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "user@pidex.ca",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },
        {
          "subcat_id": "23",
          "name": "Restorant23",
          "logo": "logo23",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "user@pidex.ca",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },

      ]
    },
    {
      "cat_id": "03",
      "name": "Shop",
      "logo": "catagoryLogo3",
      "lon": "72.1500",
      "sub_catagories": [
        {
          "subcat_id": "31",
          "name": "Shop31",
          "logo": "logo31",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "user@pidex.ca",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },
        {
          "subcat_id": "32",
          "name": "Shop32",
          "logo": "logo32",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "user@pidex.ca",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },
        {
          "subcat_id": "33",
          "name": "Shop33",
          "logo": "logo33",
          "lat": "21.7600",
          "lon": "72.1500",
          "detail": {
            "street": "stres-1",
            "city": "Amd",
            "state": "Guj",
            "zipcode": "364005",
            "description": "Restorant Description",
            "services": "Rest services",
            "working_hour": "08",
            "contact_no": "0792431233",
            "e-mail": "user@pidex.ca",
            "website": "www.pidex.com",
            "image": [
              "img0",
              "img1",
              "img2",
              "img3",
              "img4",
              "img5",
              "img6",
              "img7"
            ]
          }
        },

      ]
    }
  ]
}

这里我有一个 JSON 字符串。如何解析并将其存储在这个特定的类对象中?

public static void getDomainData() {

    try {
        JSONObject jsonobj = new JSONObject(Result);
        BusinessAllDataClass AllData = new BusinessAllDataClass();

        if (jsonobj.has(Constants.Cat)) {
            List<CategoryDataClass> Cat_list = new ArrayList<CategoryDataClass>();
            CatData = jsonobj.getJSONArray(Constants.Cat);

            for (int i = 0; i < CatData.length(); i++) {

                CategoryDataClass Cat = new CategoryDataClass();
                JSONObject objCat = CatData.getJSONObject(i);
                if (objCat.has(Constants.Cat_ID)) {
                    Cat.setCatId(objCat.getString(Constants.Cat_ID));
                }
                if (objCat.has(Constants.Cat_NAME)) {
                    Cat.setCatName(objCat.getString(Constants.Cat_NAME));
                }
                if (objCat.has(Constants.Cat_Logo)) {
                    Cat.setCatLogo(objCat.getString(Constants.Cat_Logo));

                }
                if (objCat.has(Constants.Sub_Cat)) {
                    SubCat_list = new ArrayList<SubCategoryDataClass>();
                    SubCatData = objCat.getJSONArray(Constants.Sub_Cat);

                    for (int j = 0; j < 3; j++) {

                        SubCategoryDataClass SubCat = new SubCategoryDataClass();
                        JSONObject objSubCat = SubCatData.getJSONObject(j);
                        if (objSubCat.has(Constants.Sub_CatID)) {
                            SubCat.setSubCatId(objSubCat
                                    .getString(Constants.Sub_CatID));

                        }
                        if (objSubCat.has(Constants.Sub_CatNAME)) {
                            SubCat.setSubCatName(objSubCat
                                    .getString(Constants.Sub_CatNAME));

                        }
                        if (objSubCat.has(Constants.Sub_CatLogo)) {
                            SubCat.setSubCatLogo(objSubCat
                                    .getString(Constants.Sub_CatLogo));

                        }
                        if (objSubCat.has(Constants.Longitude)) {
                            SubCat.setLongitude(objSubCat
                                    .getString(Constants.Longitude));

                        }
                        if (objSubCat.has(Constants.Latitudde)) {
                            SubCat.setLatitiude(objSubCat
                                    .getString(Constants.Latitudde));

                        }
                        if (objSubCat.has(Constants.Description)) {
                            List<SubCategorDescriptionDataClass> SubCatDescription_list = new ArrayList<SubCategorDescriptionDataClass>();
                            JSONObject DescObj = objSubCat
                                    .getJSONObject(Constants.Description);
                            SubCategorDescriptionDataClass SubCatDesc = new SubCategorDescriptionDataClass();
                            if (DescObj.has(Constants.Street)) {
                                SubCatDesc.setStreet(DescObj
                                        .getString(Constants.Street));

                            }
                            if (DescObj.has(Constants.City)) {
                                SubCatDesc.setCity(DescObj
                                        .getString(Constants.City));

                            }
                            if (DescObj.has(Constants.State)) {
                                SubCatDesc.setState(DescObj
                                        .getString(Constants.State));

                            }
                            if (DescObj.has(Constants.Zipcode)) {
                                SubCatDesc.setzipcode(DescObj
                                        .getString(Constants.Zipcode));

                            }
                            if (DescObj.has(Constants.DataDescription)) {
                                SubCatDesc
                                        .setDesc(DescObj
                                                .getString(Constants.DataDescription));

                            }

                            if (DescObj.has(Constants.Services)) {
                                SubCatDesc.setServices(DescObj
                                        .getString(Constants.Services));
                            }
                            if (DescObj.has(Constants.WorkingHour)) {
                                SubCatDesc.setWorkingHour(DescObj
                                        .getString(Constants.WorkingHour));
                            }
                            if (DescObj.has(Constants.Contact_No)) {
                                SubCatDesc.setContactNo(DescObj
                                        .getString(Constants.Contact_No));
                            }
                            if (DescObj.has(Constants.Usre_Email)) {
                                SubCatDesc.setUsreEmail(DescObj
                                        .getString(Constants.Usre_Email));
                            }
                            if (DescObj.has(Constants.Website)) {
                                SubCatDesc.setWebsite(DescObj
                                        .getString(Constants.Website));
                            }
                            if (DescObj.has(Constants.Image)) {
                                DescImage = DescObj
                                        .getJSONArray(Constants.Image);
                                Log.e("JsonArray",
                                        String.valueOf(DescImage.length()));
                                for (int k = 0; k < DescImage.length(); k++) {

                                    SubCatDesc.ImageData.add(DescImage
                                            .getString(k));

                                }
                                SubCatDescription_list.add(SubCatDesc);

                                SubCat.setSubCatDescList(SubCatDescription_list);

                            }
                            SubCat_list.add(SubCat);

                            Cat.setSubCatList(SubCat_list);
                        }
                    }
                }

                Cat_list.add(Cat);

            }
            AllData.setCatList(Cat_list);

        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
4

3 回答 3

2

尝试使用包含类似 JSON 响应的GSON Parsing教程。

于 2013-10-11T13:14:26.900 回答
2

你应该使用 gson 解析器。Gson lib 谷歌的产品和非常好的库。只有你声明类和 json 数据。

Gson gson = new Gson();
Object result = gson.fromJSON(jsonData, object);

但是您将类字段声明为与 json 数据相同。比如你的类必须包含类别数组。(同名)

于 2013-10-11T13:11:26.500 回答
0

在这里,我声明SubCat_liststatic所以我没有得到索引值:

public static void getDomainData() {

    try {
        JSONObject jsonobj = new JSONObject(Result);
        BusinessAllDataClass AllData = new BusinessAllDataClass();

        if (jsonobj.has(Constants.Key_CATEGORY)) {
            List<CategoryDataClass> Category_list = new ArrayList<CategoryDataClass>();
            JSONArray CatData = jsonobj
                    .getJSONArray(Constants.Key_CATEGORY);

            for (int i = 0; i < CatData.length(); i++) {
                if (CatData.getJSONObject(i).length() > 0) {
                    CategoryDataClass Cat = new CategoryDataClass();
                    JSONObject objCat = CatData.getJSONObject(i);
                    if (objCat.has(Constants.Key_CATEGORY_ID)) {
                        Cat.setCatId(objCat
                                .getString(Constants.Key_CATEGORY_ID));
                    }
                    if (objCat.has(Constants.Key_CATEGORY_NAME)) {
                        Cat.setCatName(objCat
                                .getString(Constants.Key_CATEGORY_NAME));
                    }
                    if (objCat.has(Constants.Key_CATEGORY_LOGO)) {
                        Cat.setCatLogo(objCat
                                .getString(Constants.Key_CATEGORY_LOGO));

                    }
                    if (objCat.has(Constants.Key_SUBCATEGORY)) {
                        List<SubCategoryDataClass> SubCategory_list = new ArrayList<SubCategoryDataClass>();
                        JSONArray SubCatData = objCat
                                .getJSONArray(Constants.Key_SUBCATEGORY);

                        for (int j = 0; j < 3; j++) {
                            if (SubCatData.getJSONObject(j).length() > 0) {
                                SubCategoryDataClass SubCat = new SubCategoryDataClass();
                                JSONObject objSubCat = SubCatData
                                        .getJSONObject(j);
                                if (objSubCat
                                        .has(Constants.Key_SUBCATEGORY_ID)) {
                                    SubCat.setSubCatId(objSubCat
                                            .getString(Constants.Key_SUBCATEGORY_ID));

                                }
                                if (objSubCat
                                        .has(Constants.Key_SUBCATEGORY_NAME)) {
                                    SubCat.setSubCatName(objSubCat
                                            .getString(Constants.Key_SUBCATEGORY_NAME));

                                }
                                if (objSubCat
                                        .has(Constants.Key_SUBCATEGORY_LOGO)) {
                                    SubCat.setSubCatLogo(objSubCat
                                            .getString(Constants.Key_SUBCATEGORY_LOGO));

                                }
                                if (objSubCat
                                        .has(Constants.Key_SUBCATEGORY_LONGITUDE)) {
                                    SubCat.setLongitude(objSubCat
                                            .getString(Constants.Key_SUBCATEGORY_LONGITUDE));

                                }
                                if (objSubCat
                                        .has(Constants.Key_SUBCATEGORY_LATITUDE)) {
                                    SubCat.setLatitiude(objSubCat
                                            .getString(Constants.Key_SUBCATEGORY_LATITUDE));

                                }
                                if (objSubCat.has(Constants.Key_DETAIL)) {
                                    List<SubCategorDescriptionDataClass> SubCatDescription_list = new ArrayList<SubCategorDescriptionDataClass>();
                                    JSONObject DescObj = objSubCat
                                            .getJSONObject(Constants.Key_DETAIL);
                                    SubCategorDescriptionDataClass SubCatDesc = new SubCategorDescriptionDataClass();
                                    if (DescObj.has(Constants.Key_STREET)) {
                                        SubCatDesc
                                                .setStreet(DescObj
                                                        .getString(Constants.Key_STREET));

                                    }
                                    if (DescObj.has(Constants.Key_CITY)) {
                                        SubCatDesc
                                                .setCity(DescObj
                                                        .getString(Constants.Key_CITY));

                                    }
                                    if (DescObj.has(Constants.Key_STATE)) {
                                        SubCatDesc
                                                .setState(DescObj
                                                        .getString(Constants.Key_STATE));

                                    }
                                    if (DescObj.has(Constants.Key_ZIPCODE)) {
                                        SubCatDesc
                                                .setzipcode(DescObj
                                                        .getString(Constants.Key_ZIPCODE));

                                    }
                                    if (DescObj
                                            .has(Constants.Key_DESCRIPTION)) {
                                        SubCatDesc
                                                .setDesc(DescObj
                                                        .getString(Constants.Key_DESCRIPTION));

                                    }

                                    if (DescObj.has(Constants.Key_SERVICES)) {
                                        SubCatDesc
                                                .setServices(DescObj
                                                        .getString(Constants.Key_SERVICES));
                                    }
                                    if (DescObj
                                            .has(Constants.Key_WORKINGHOUR)) {
                                        SubCatDesc
                                                .setWorkingHour(DescObj
                                                        .getString(Constants.Key_WORKINGHOUR));
                                    }
                                    if (DescObj
                                            .has(Constants.Key_CONTACT_NO)) {
                                        SubCatDesc
                                                .setContactNo(DescObj
                                                        .getString(Constants.Key_CONTACT_NO));
                                    }
                                    if (DescObj
                                            .has(Constants.Key_USER_EMAIL)) {
                                        SubCatDesc
                                                .setUsreEmail(DescObj
                                                        .getString(Constants.Key_USER_EMAIL));
                                    }
                                    if (DescObj.has(Constants.Key_WEBSITE)) {
                                        SubCatDesc
                                                .setWebsite(DescObj
                                                        .getString(Constants.Key_WEBSITE));
                                    }
                                    if (DescObj.has(Constants.Key_IMAGE)) {
                                        JSONArray DescImage = DescObj
                                                .getJSONArray(Constants.Key_IMAGE);

                                        for (int k = 0; k < DescImage
                                                .length(); k++) {

                                            SubCatDesc.ImageData
                                                    .add(DescImage
                                                            .getString(k));

                                        }
                                        SubCatDescription_list
                                                .add(SubCatDesc);

                                        SubCat.setSubCatDescList(SubCatDescription_list);
                                    }
                                }
                                SubCategory_list.add(SubCat);

                                Cat.setSubCatList(SubCategory_list);
                            }
                        }

                    }
                    Category_list.add(Cat);
                }
            }

            AllData.setCatList(Category_list);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
于 2013-10-14T13:00:09.333 回答