How to Parse JSONarray inside JSONarray in android?

后端 未结 1 1540
时光说笑
时光说笑 2021-01-07 08:11

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

Now I am not getting images please Help. IMAGES, DET

相关标签:
1条回答
  • 2021-01-07 09:02

    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);
            }
        }
    
    0 讨论(0)
提交回复
热议问题