【问题标题】:Parse Json Array Inside Json Array with android使用android解析Json数组内部的Json数组
【发布时间】:2015-06-23 16:34:43
【问题描述】:

我正在尝试使用 android 制作扩展列表视图,这是我的 json 结果。

[
  {
    eCode: 0,
    Menu_Cat_Id: 1,
    Menu_Cat_Name: "Salaaad",
    photo: "http://awtuts.com/restaurant/uploads/categories/2.jpg",
    subCategories: [
      {
        Item_Id: 1,
        Item_NameAR: "شيش طاووق",
        Item_NameEN: "SHeeeeeeesh",
        Item_size: "Larg",
        Item_Prc: 100,
        Item_Photo: "http://awtuts.com/restaurant/uploads/subcategories/1.jpg"
      },
      {
        Item_Id: 1,
        Item_NameAR: "شيش طاووق",
        Item_NameEN: "SHeeeeeeesh",
        Item_size: "Small",
        Item_Prc: 10,
        Item_Photo: "http://awtuts.com/restaurant/uploads/subcategories/1.jpg"
      }
    ]
  },
  {
    eCode: 0,
    Menu_Cat_Id: 2,
    Menu_Cat_Name: "Salaaad",
    photo: "http://awtuts.com/restaurant/uploads/categories/2.jpg",
    subCategories: [

    ]
  }
]

我的安卓代码是:

 protected void onPostExecute(String result) {
            try {
                Parent recievedUser;
                Parent child;
                ArrayList<Parent> listDataHeader = new ArrayList<Parent>();
                List<String> parents = new ArrayList<>();
                ArrayList<Parent> listChildData = new ArrayList<Parent>();
                List<String> children = new ArrayList<>();
                JSONArray jsonArray = new JSONArray(result);
                JSONArray jsonArrayChild=new JSONArray(result);
                for (int i = 0; i < jsonArray.length(); i++) {
                    jsonObject = jsonArray.getJSONObject(i);
                    recievedUser = new Parent();
                    child=new Parent();
                    recievedUser.setMenu_Cat_Id(jsonObject.getInt("Menu_Cat_Id"));
                    recievedUser.setMenu_Cat_Name(jsonObject.getString("Menu_Cat_Name"));
                    recievedUser.setPhoto(jsonObject.getString("photo"));
                    child.setItem_NameEN(jsonObject.getString("Item_NameEN"));
                    listDataHeader.add(recievedUser);
                    listChildData.add(child);

                }

                // set list adapter
                ExpandableListAdapter expandableListAdapter = new ExpandableListAdapter(MainActivity.this, listDataHeader, listChildData);
                expListView.setAdapter(expandableListAdapter);

但我有一个例外,儿童列表有问题。 我已经尝试了很多东西,但它们对我不起作用,请任何人帮助我找到我的代码的错误之处吗?

【问题讨论】:

  • 使用jsonArray.getJSONArray()
  • 请问如何使用?
  • 我的 Json api 是 Post 类型
  • @DinaShaldoum:首先发布带有问题的有效 json 以获得更多帮助
  • 它对我不起作用,android 告诉我不能使用 jsonArray。

标签: android arrays json expandablelistview expandablelistadapter


【解决方案1】:

试试这样的:

JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
  jsonObject = jsonArray.getJSONObject(i);
  recievedUser = new Parent();
  recievedUser.setMenu_Cat_Id(jsonObject.getInt("Menu_Cat_Id"));        
  recievedUser.setMenu_Cat_Name(jsonObject.getString("Menu_Cat_Name"));
  recievedUser.setPhoto(jsonObject.getString("photo"));
  categories = new JSONArray(jsonObject.get("categories"));

  for (int i = 0; i < categories.length(); i++) {
   category = categories.getJSONobject(i);
   child=new Parent();
   child.setItem_NameEN(categories.getString("Item_NameEN"));
   listChildData.add(child);  
  }

  listDataHeader.add(recievedUser);          
}

【讨论】:

  • 如何获取片段中的数据和子类别?我创建了 2 个存储嵌套 JSONArray 的类..?我无法从片段中的子类别中获取值。请帮忙。
【解决方案2】:

我已经为您的响应解析检查创建了一种方法。成功解析您的 jsonarray 响应也获得子类别响应。

检查下面的方法并在下面的方法中解析你的 json 数组字符串。如果有任何问题,请添加评论

 public void parseJsonResponse(String response) {
        try {

            JSONArray jsonArray = new JSONArray(response);

            if (jsonArray != null && jsonArray.length() > 0) {
                for (int cnt = 0; cnt < jsonArray.length(); cnt++) {
                    JSONObject jsonObj = null;
                    jsonObj = jsonArray.getJSONObject(cnt);
                    Log.d("tag", "jsonObj Menu_Cat_Id->" + jsonObj.optString("Menu_Cat_Name"));
                    JSONArray jsonArrSubCat = jsonObj.getJSONArray("subCategories");
                    if (jsonArrSubCat != null && jsonArrSubCat.length() > 0) {

                        for (int subCnt = 0; subCnt < jsonArrSubCat.length(); subCnt++) {
                            JSONObject jsonObjSubCategory = jsonArrSubCat.getJSONObject(subCnt);
                            Log.d("tag", "jsonArrSubCat Item_NameEN->" + jsonObjSubCategory.optString("Item_NameEN"));
                        }
                    }
                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

我正在低于日志并成功解析您的 json 响应,没有任何错误

06-24 00:32:39.205  24881-24881/? D/tag﹕ jsonObj Menu_Cat_Id->Salaaad
06-24 00:32:39.205  24881-24881/? D/tag﹕ jsonArrSubCat Item_NameEN->SHeeeeeeesh
06-24 00:32:39.205  24881-24881/? D/tag﹕ jsonArrSubCat Item_NameEN->SHeeeeeeesh
06-24 00:32:39.205  24881-24881/? D/tag﹕ jsonObj Menu_Cat_Id->Salaaad

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 2013-01-11
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    相关资源
    最近更新 更多