如何解析JSONarray内JSONarray android系统中? [英] How to Parse JSONarray inside JSONarray in android?

查看:96
本文介绍了如何解析JSONarray内JSONarray android系统中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨每一个我跟着的1 从URL,但在分析的时候,我strucked在一个地方解析JSON .. 为

Hi every one I followed this one to parse JSON from a URL but at the time of parsing I strucked at one place.. as

现在我没有收到图片,请帮助。图像,细节,为您提供那些没有得到.. 他们在jsonarray(JSONObject的(jsonarray(的JSONObject))),请帮助

Now I am not getting images please Help. IMAGES, DETAILS ,OFFERS those are not Getting .. They are in jsonarray(jsonobject(jsonarray(jsonobject))) please help

我有一个低于阵列内的图像

I have a image inside a array like below

{
"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"
            ],
            ..
        }
    ],
    ...
   }
   }

现在我要如何解析一些像图像的内容,newoffers这是内部jsonarray(JSONArray),其中我的意思是,他们都喜欢[{[{和[{JSON数组中[JSON数组我试过,但我在收到错误

Now I want how to parse some of the contents like Images, newoffers which are inside jsonarray(jsonarray) i mean they are like [{ [{ and [{[ json array inside json array I tried but I am getting error at

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

这是我的code ..

This is my code..

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);
            }     
    }
  }
}

remaning个个都是精品。但我无法解析的图像,细节,并提供..请帮助。 这三个是jsonarray内(JSONObject的(Jsonarray(的JSONObject)))这是事.. 请帮助..

remaning all are fine. but I am unable to parse images,details,and offers.. please help. Those three are inside the jsonarray(jsonObject (Jsonarray(jsonobject))) this is the thing.. Please help..

推荐答案

我创建了一个不完整的片段。与您的给定的例子可以访问至少到第一new_offers,SKU等,这是不完整的,但因为它会失败,如果一个字段不存在。反正片段的目的只是向您展示如何从一个嵌套的JSONObject和JSONArray值

I created an incomplete snippet. With your given example it can access at least up to the first new_offers, sku, etc. This is incomplete though as it will fail if a field does not exist. Anyway the purpose of the snippet is just to show you how to retrieve the value from a nested JSONObject and 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);
        }
    }

这篇关于如何解析JSONarray内JSONarray android系统中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆