【问题标题】:How to Parse JSONarray inside JSONarray in android?如何在 android 中解析 JSONarray 中的 JSONarray?
【发布时间】:2013-12-20 08:52:13
【问题描述】:

嗨,我关注的每一位this 一个 从 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) 中的图像、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);
            }     
    }
  }
}

remaning 一切都很好。但我无法解析图像、细节和优惠……请帮忙。 这三个在 jsonarray(jsonObject (Jsonarray(jsonobject))) 里面,这就是事情.. 请帮忙..

【问题讨论】:

  • 看看gson和例子,这会很容易。
  • 请给我关于上述示例的答案,我的意思是来自这个 url (androidbegin.com/tutorial/…) 而不是 GSON.. 我将尝试下一个 GSON 请告诉我如何解决图像...
  • @user3073115 你使用的是你定义的那个教程的同一个链接吗?
  • 你能用一个有效的json例子吗?

标签: android json url


【解决方案1】:

我创建了一个不完整的 sn-p。对于您给定的示例,它至少可以访问第一个 new_offers、sku 等。这是不完整的,因为如果字段不存在,它将失败。无论如何,sn-p 的目的只是向您展示如何从嵌套的 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);
        }
    }

【讨论】:

  • JSONArray json_results_images = json_results.getJSONArray("images"); 应该是JSONArray json_results_images = c.getJSONArray("images");
  • 好的先生,先生,细节和报价怎么样..请帮助..只有两件事
猜你喜欢
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
  • 2017-05-01
  • 1970-01-01
相关资源
最近更新 更多