【问题标题】:How to parse this nested JSON array in android如何在android中解析这个嵌套的JSON数组
【发布时间】:2013-07-14 10:16:39
【问题描述】:

我必须将下面嵌套的 Json 数组的数据解析到我的应用程序中。我很困惑如何从中获得价值。

  {
            "prodCat_list": [
                {
                    "prods": [
                        {
                            "cat_id": "9",
                            "position": "1",
                            "sku": "wwww345"

                        },
                        {
                            "cat_id": "9",
                            "position": "2",
                            "sku": "coof23"

                        },
                        {
                            "cat_id": "9",
                            "position": "3",
                            "sku": "dde45"

                        },
                        {
                            "cat_id": "9",
                            "position": "4",
                            "sku": "5555"

                        }
             ]
                },
{
                    "prods": [
                        {
                            "cat_id": "9",
                            "position": "1",
                            "sku": "wwww345"

                        },
                        {
                            "cat_id": "9",
                            "position": "2",
                            "sku": "coof23"

                        },
                        {
                            "cat_id": "9",
                            "position": "3",
                            "sku": "dde45"

                        },
                        {
                            "cat_id": "9",
                            "position": "4",
                            "sku": "5555"

                        }
             ]
                },
            ]
        }

谁能指导我如何从中获得内部价值。

我试过了

JSONParser parser = new JSONParser();
        JSONObject items = parser.getJSONFromUrl(productInfoUrl);
        try {
            JSONArray itemsDetails = items.getJSONArray("prodCat_list");
            if(itemsDetails.length()>0){

                for (int i = 0; i < itemsDetails.length(); i++) {
                    JSONArray productWithCategories = itemsDetails.getJSONArray(i);
                    JSONObject object = productWithCategories.getJSONObject(i);

                    Product productInfo = new Product( object.getString("sku"), object.getInt("cat_id"), object.getInt("position"));
                    ProductDbHandler productDbHandler = new ProductDbHandler(context);
                    productDbHandler.addProducts(productInfo);
                }
            }
            else 
                System.out.println("No product to add");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

【问题讨论】:

  • @Enrichman 好的,我会告诉你的。
  • 顺便说一句,那个 json 无效。内部对象中的最后一项以逗号结尾:"sku": "5555",
  • @Enrichman 我已经添加了尝试过的代码。我也删除了那个逗号
  • 仍然无效。您的对象末尾有一个逗号。使用 jsonlint.com 检查您的 json。并检查 logcat 是否有任何错误。
  • 您的 json 文件中还有一个逗号 (,)(倒数第三行),请将其删除并重试..

标签: android json arrays


【解决方案1】:

我认为您的 JSON 解析器应该是这样的(可能有一些拼写错误,我没有在编辑器上测试此代码:)):

JSONObject mainObj = new JSONOBject(myString);
if(mainObj != null){
    JSONArray list = mainObj.getJSONArray("prodCat_list");
    if(list != null){
        for(int i = 0; i < list.length();i++){
            JSONObject elem = list.getJSONObject(i);
            if(elem != null){
                JSONArray prods = elem.getJSONArray("prods");
                if(prods != null){
                    for(int j = 0; j < prods.length();j++){
                        JSONObject innerElem = prods.getJSONObject(j);
                        if(innerElem != null){
                            int cat_id = innerELem.getInt("cat_id");
                            int pos = innerElem.getInt("position");
                            String sku = innerElem.getString("sku");
                        }
                    }
                }
            }
        }
    }
}

【讨论】:

  • 我会测试它。谢谢
  • 感谢您的帮助:)
  • 不错..它也帮助了我..感谢您的回答.. :)..我有一个问题..这是解析嵌套数组/对象的最佳方法吗?
  • 这很大程度上取决于你的 JSON 字符串,但在我看来,这是我所知道的最好的解决方案。
【解决方案2】:

您可以使用 GSon 库。它将 json 解析为您可以访问的各种对象。 一个虚拟代码在这里:

` GSon gSon  = new GSon();
  ProdCatList prodCatList = gSon.fromJson(---inputStreamReader of your JSon data---,ProdCatList.class);`

【讨论】:

    【解决方案3】:

    试试这个!

    //Write your own implementation of json parser
    JSONParser jParser = new JSONParser();
    JSONArray prod_cat = new JSONArray();
    JSONArray products = new JSONArray();
    JSONObject json = jParser.getJSONFromUrl("your source");
    prod_cat = json.getJSONArray("prodCat_list");
    for (int i = 0; i < prod_cat.length(); i++) {
            JSONObject object = prod_cat.getJSONObject(i);
            products = object.getJSONArray("products");
    }
    

    【讨论】:

      【解决方案4】:

      尝试使用

      org.json.simple.JSONValue
      

      代码:

      import org.json.simple.JSONValue;
      String content = "{...}";
      JSONValue.parse(content);
      

      【讨论】:

        【解决方案5】:

        JSONObject reader = new JSONObject(results);

                    JSONArray VendorProductsList = reader.getJSONArray("VendorProductsList");
                    for (int i = 0; i < VendorProductsList.length(); i++) {
                        ebay=new Ebay();
                        JSONObject elem = VendorProductsList.getJSONObject(i);
                        JSONArray Products = elem.getJSONArray("Products");
        
                        for (int j = 0; j < Products.length(); j++) {
                            JSONObject innerElem = Products.getJSONObject(j);
                           //JSONObject ProductName=Products.getJSONObject(j);
                          ebay.setProductName(innerElem.getString("ProductName"));
                            gallery_array.add(ebay);
        

        【讨论】:

          【解决方案6】:

          只需用 Gson 自动解析即可:

          JSONParser parser = new JSONParser();
          JSONObject items = parser.getJSONFromUrl(productInfoUrl);
          Gson gson = new GsonBuilder().create();
          ProdCatResponse prodCatResponse = gson.fromJson(items.toString(), ProdCatResponse.class);
          

          但实际上你可以解析字符串形式的 JSON 响应。您无需在JSONObject 中获取响应并使用toString()

          JSONParser parser = new JSONParser();
          String items = parser.getFromUrl(productInfoUrl);
          Gson gson = new GsonBuilder().create();
          ProdCatResponse prodCatResponse = gson.fromJson(items, ProdCatResponse.class);
          

          更多关于 Gson 的信息:https://guides.codepath.com/android/Leveraging-the-Gson-Library


          这是模型类示例,你可以用http://www.jsonschema2pojo.org生成它。

          ProdCatResponse.java

          public class ProdCatResponse {
          
              private List <Prod> prods = new ArrayList <Prod> ();
          
              public List <Prod> getProds() {
                  return prods;
              }
          
              public void setProds(List < Prod > prods) {
                  this.prods = prods;
              }
          
          }
          

          Prod.java

          public class Prod {
          
              private String catId;
              private String position;
              private String sku;
          
              public String getCatId() {
                  return catId;
              }
          
              public void setCatId(String catId) {
                  this.catId = catId;
              }
          
              public String getPosition() {
                  return position;
              }
          
              public void setPosition(String position) {
                  this.position = position;
              }
          
              public String getSku() {
                  return sku;
              }
          
              public void setSku(String sku) {
                  this.sku = sku;
              }
          
          }
          

          【讨论】:

            猜你喜欢
            • 2021-01-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-03-17
            相关资源
            最近更新 更多