繁体   English   中英

如何在Android中解析JSONarray内的JSONarray?

[英]How to Parse JSONarray inside JSONarray in android?

大家好,我遵循一步来从URL解析JSON,但是在解析时,我碰到了一个地方。

现在我没有图像,请帮助。 图像,详细信息,优惠这些没有得到..它们在jsonarray(jsonobject(jsonarray(jsonobject)))中,请帮助

我在如下数组中有一个图像

{
"returnCode": "success",
"Data": {
    "results": [
        {
            "moredetails": [
                {
                    "newoffers": [

                    ],
                    "recentoffers_count": 0,
                    "sku": "30072246"
                },
                {
                    "newoffers": [
                        {
                            "availability": "Available",
                            "currency": "USD"
                        }
                    ]
                },
                {
                    "newoffers": [
                        {
                            "availability": "Available",
                            "currency": "USD"
                        }
                    ],
                    "offers_count": 1,
                    "name": "google.com"
                }
            ],
            ..."features": {
                ..
            },
            "length": "20",
            "geo": [
                "usa"
            ],
            .."images": [
                "http://google.com.one.jpg"
            ],
            ..
        }
    ],
    ...
   }
   }

现在我想如何解析jsonarray(jsonarray)内部的Images,newoffers等内容,我的意思是它们就像[{[{和[{[我尝试过的json数组内的json数组,但是我在

JSONArray json_results = json_results.getJSONArray("images");

这是我的代码。

try {
JSONObject json_data = JSONfunctions.getJSONfromURL(url);
JSONObject json_SemanticS3ProductData = json_data.getJSONObject("Data");
JSONArray json_results = json_SemanticS3ProductData.getJSONArray("results");
    for (int i = 0; i < json_SemanticS3ProductData.length(); i++) {
    HashMap<String, String> map = new HashMap<String, String>();
    JSONObject c = json_results.getJSONObject(i);
    map.put("model", c.optString("model"));

    JSONObject f = c.getJSONObject("features");
    map.put("Rear-facing Camera", f.optString("Rear-facing Camera"));

        JSONArray json_results_images = json_results.getJSONArray("images");            
        arraylist.add(map);

            JSONArray json_sitedetails =json_results.getJSONArray("details");
            for (int j = 0; j < json_sitedetails.length(); j++) {
            HashMap<String, String> map1 = new HashMap<String, String>();
            JSONObject sd = json_sitedetails.getJSONObject(j);
            JSONArray json_latestoffers = json_sitedetails.getJSONArray("offers");
            for (int k = 0; k < json_sitedetails.length(); k++) {
            HashMap<String, String> map2 = new HashMap<String, String>();
            JSONObject e = json_latestoffers.getJSONObject(k);
            map1.put("currency", e.optString("currency"));                    
            arraylist.add(map1);
            }     
    }
  }
}

剩下的都还好。 但我无法解析图像,详细信息和报价。.请帮助。 这三个都在jsonarray(jsonObject(Jsonarray(jsonobject)))里面。..请帮助..

我创建了一个不完整的代码段。 在给定的示例中,它至少可以访问第一个new_offers,sku等。这是不完整的,但是如果一个字段不存在,它将失败。 无论如何,此代码段的目的只是向您展示如何从嵌套的JSONObject和JSONArray中检索值。

    JSONObject root = new JSONObject(sample);
    String returnCode = root.getString("returnCode");
    Log.d(TAG, "returnCode:" + returnCode);

    JSONObject data = root.getJSONObject("Data");
    JSONArray results = data.getJSONArray("results");
    int resultsSize = results.length();
    for (int i = 0; i < resultsSize; i++) {
        JSONObject result = results.getJSONObject(i);
        JSONArray moreDetails = result.getJSONArray("moredetails");
        for (int i2 = 0; i2 < moreDetails.length(); i2++) {
            JSONObject detail = moreDetails.getJSONObject(i2);
            JSONArray newOffers = detail.getJSONArray("newoffers");

            int recentOffersCount = detail.getInt("recentoffers_count");
            String sku = detail.getString("sku");

            Log.d(TAG, "recentOffersCount:"+recentOffersCount);
            Log.d(TAG, "sku:"+sku);

            // Will fail on the 2nd moreDetail because the field is incomplete. be sure to handle it.
        }
        JSONObject features = result.getJSONObject("features");
        int length = result.getInt("length");
        JSONArray geo = result.getJSONArray("geo");
        JSONArray images = result.getJSONArray("images");
        for(int i2 = 0; i2 < images.length();i2++) {
            String image = images.getString(i2);
            Log.d(TAG, "image:"+image);
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM