如何从android中的json解析可选值

时间:2014-06-14 11:02:36

标签: android json arrays

我已经创建了一个android应用程序,因为我想从api解析一些数据,我在api中有一些可选值,Sample如下: 的 JSON

{
status: "success",
product_category: {
name: "Glossy Mannequins",
id: "28"
},
product: [
{
name: "3SC1G",
id: "128",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372498306.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1400929422.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
product_options: [
{
option_name: "Color",
option_value: "White"
},
{
option_name: "Height",
option_value: "130 cm"
}
]
},
{
name: "3S14FG",
id: "127",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372497779.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1400929548.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "3S13FG",
id: "126",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372496302.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401176927.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "3S12MG",
id: "125",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372496174.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401176821.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "3S3FG",
id: "124",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372306217.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401176740.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "3S9FG",
id: "123",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372305991.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401176646.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "3S5MG",
id: "117",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372304662.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401176538.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "3S8MG",
id: "116",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372304573.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401176461.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "3S10MG",
id: "115",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1372304511.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401176396.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "M12G",
id: "99",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368463047.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401180875.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "M10G",
id: "98",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368462841.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401180931.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "M8G",
id: "97",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368462735.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401180955.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "M5G",
id: "96",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368462593.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401181016.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "M1G",
id: "95",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368452614.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401180978.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "F13GB",
id: "93",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368451347.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401181053.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "F9GB",
id: "92",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368450963.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401181078.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "F3GB",
id: "91",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368450873.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401181131.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
},
{
name: "C1G",
id: "90",
thumb_image: "http://3smannequins.com/uploads/product_image/mediumthumb1368450534.jpg",
big_image: "http://3smannequins.com/uploads/product_image/1401181181.jpg",
product_subcategory: {
name: "Glossy Mannequins",
id: "28"
},
msg_option: "not_available"
}
]
}

在上面的JSON我想要解析“产品选项”的值...这不是在所有对象中,我尝试了“optJSONArray”但没有得到解决方案,我试过如下,哪个不起作用,请帮我解决这个问题.. 的 main.jaa

try {
                if (jsonStr != null) {

                    jsonObj = new JSONObject(jsonStr);

                    if (jsonObj.has("product")) {

                        subCategories = jsonObj.getJSONArray("product");
                        if (subCategories != null
                                && subCategories.length() != 0) {

                            // looping through All Contacts

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

                                JSONObject c = subCategories.getJSONObject(i);

                                sub_id = c.getString("id");
                                sub_name = c.getString("name");

                                thumb_image = c.getString("thumb_image");
                                big_image = c.getString("big_image");
                                resultArray.add(big_image);

                                if (c.has("product_options")) {
                                    JSONArray product_optionsarray = c
                                            .optJSONArray("product_options");

                                    for (int j = 0; j < product_optionsarray
                                            .length(); j++) {

                                        JSONObject c1 = product_optionsarray
                                                .optJSONObject(j);

                                        if (c1.has("option_name"))
                                            option_name = c1
                                                    .optString("option_name");
                                        System.out
                                                .println("::::::::::::::::Option name::::::"
                                                        + option_name);

                                        if (c1.has("option_value"))
                                            option_value = c1
                                                    .optString("option_value");
                                        System.out
                                                .println("::::::::::::::::Optionvalue::::::"
                                                        + option_value);
                                    }
                                }
                                HashMap<String, String> subCategory = new HashMap<String, String>();

                                subCategory.put(Const.TAG_CATEGORY_ID, sub_id);
                                subCategory.put(Const.TAG_CATAEGORY_NAME,
                                        sub_name);
                                subCategory.put(Const.TAG_THUMB_IMG,
                                        thumb_image);
                                subCategory.put(Const.TAG_BIG_IMG, big_image);
                                subCategory.put(Const.TAG_OPTION_NAME,
                                        option_name);
                                subCategory.put(Const.TAG_OPTION_VALUE,
                                        option_value);

                                subCatList.add(subCategory);

                            }
                        }

                    }

2 个答案:

答案 0 :(得分:1)

Try this way,hope this will help you to solve your problem.

        String jsonStr = "{status:\"success\",product_category:{name:\"Glossy Mannequins\",id:\"28\"},product:[{name:\"3SC1G\",id:\"128\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1372498306.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1400929422.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},product_options:[{option_name:\"Color\",option_value:\"White\"},{option_name:\"Height\",option_value:\"130 cm\"}]},{name:\"3S14FG\",id:\"127\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1372497779.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1400929548.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"3S13FG\",id:\"126\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1372496302.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401176927.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"3S12MG\",id:\"125\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1372496174.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401176821.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"3S3FG\",id:\"124\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1372306217.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401176740.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"3S9FG\",id:\"123\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1372305991.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401176646.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"3S5MG\",id:\"117\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1372304662.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401176538.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"3S8MG\",id:\"116\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1372304573.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401176461.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"3S10MG\",id:\"115\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1372304511.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401176396.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"M12G\",id:\"99\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1368463047.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401180875.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"M10G\",id:\"98\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1368462841.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401180931.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"M8G\",id:\"97\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1368462735.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401180955.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"M5G\",id:\"96\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1368462593.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401181016.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"M1G\",id:\"95\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1368452614.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401180978.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"F13GB\",id:\"93\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1368451347.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401181053.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"F9GB\",id:\"92\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1368450963.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401181078.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"F3GB\",id:\"91\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1368450873.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401181131.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"},{name:\"C1G\",id:\"90\",thumb_image:\"http://3smannequins.com/uploads/product_image/mediumthumb1368450534.jpg\",big_image:\"http://3smannequins.com/uploads/product_image/1401181181.jpg\",product_subcategory:{name:\"Glossy Mannequins\",id:\"28\"},msg_option:\"not_available\"}]}";
        JSONObject jsonObj;
        JSONArray subCategories;
        ArrayList<HashMap<String,String>> subCatList = new ArrayList<HashMap<String, String>>();
        ArrayList<String> resultArray = new ArrayList<String>();

        try{
            if (jsonStr != null && jsonStr.length()>0) {
                jsonObj = new JSONObject(jsonStr);
                if (jsonObj.has("product")) {
                    subCategories = jsonObj.getJSONArray("product");
                    if (subCategories != null && subCategories.length() > 0) {
                        for (int i = 0; i < subCategories.length(); i++) {
                            String sub_id;
                            String sub_name;
                            String thumb_image;
                            String big_image;
                            String option_name="";
                            String option_value="";

                            sub_id = subCategories.getJSONObject(i).getString("id");
                            sub_name = subCategories.getJSONObject(i).getString("name");
                            thumb_image = subCategories.getJSONObject(i).getString("thumb_image");
                            big_image = subCategories.getJSONObject(i).getString("big_image");
                            resultArray.add(big_image);

                            if (subCategories.getJSONObject(i).has("product_options")) {
                                JSONArray product_optionsarray = subCategories.getJSONObject(i).getJSONArray("product_options");
                                for (int j = 0; j < product_optionsarray.length(); j++) {
                                    option_name = product_optionsarray.getJSONObject(j).optString("option_name");
                                    option_value = product_optionsarray.getJSONObject(j).optString("option_value");
                                }
                            }

                            HashMap<String, String> subCategory = new HashMap<String, String>();

                            subCategory.put("ID", sub_id);
                            subCategory.put("NAME",sub_name);
                            subCategory.put("THUMB_IMG",thumb_image);
                            subCategory.put("BIG_IMG", big_image);
                            subCategory.put("OPTION_NAME",option_name);
                            subCategory.put("OPTION_VALUE",option_value);

                            subCatList.add(subCategory);

                        }
                    }

                }
            }

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

        System.out.println("Size of subCatList : "+subCatList.size());
        System.out.println("Size of resultArray : "+resultArray.size());

        for (HashMap<String,String> row : subCatList){
            System.out.println("ID : "+row.get("ID"));
            System.out.println("NAME : "+row.get("NAME"));
            System.out.println("THUMB_IMG : "+row.get("THUMB_IMG"));
            System.out.println("BIG_IMG : "+row.get("BIG_IMG"));
            System.out.println("OPTION_NAME : "+row.get("OPTION_NAME"));
            System.out.println("OPTION_VALUE : "+row.get("OPTION_VALUE"));
        }

答案 1 :(得分:0)

因为你已经检查过jsonOject是否有&#34; products_options&#34;使用if,尝试删除optJSONArray并只使用JSONArray:

if (c.has("product_options")) {
    JSONArray product_optionsarray = c.JSONArray("product_options");
    // remaining code