程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> array-如何訪問 android 中的 json 數組?

array-如何訪問 android 中的 json 數組?

編輯:編程綜合問答
如何訪問 android 中的 json 數組?

我想在程序中使用一個 json 文件。Json 文件結構如下,可以成功的訪問 json 數組 "business",但是現在我想訪問 business_cat 和 business_cat 中的cat 1 和 cat 2。我該如何訪問這些值?

{
   "business":[
     {
      "id":"13",
      "category":"Dinner",
      "subcategory":"",
      "name_eng":"dinner 1",
      "name_arab":"dinner 1",
      "mobile":"12345",
      "address":"not now",
      "logo":"1.gif",
      "contact":"Call",
      "open_time":" ",
      "close_time":" "
      }
   ],
   "business_cat":[
  [
     "cat 1",
     {
        "name":"dish 1",
        "id":"7",
        "name_arab":"dish1",
        "price":"200",
        "logo":"laptop.jpeg"
     },
     {
        "name":"dish 2",
        "id":"8",
        "name_arab":"dish 2",
        "price":"123789",
        "logo":"micky.jpg"
     },
     {
        "name":"qaz",
        "id":"10",
        "name_arab":"zaq",
        "price":"12",
        "logo":"watch.jpg"
     },
     {
        "name":"wsx",
        "id":"11",
        "name_arab":"xsw",
        "price":"12",
        "logo":"micky.jpg"
     },
     {
        "name":"zxc",
        "id":"12",
        "name_arab":"vcxz",
        "price":"34",
        "logo":"camera.jpg"
     }
  ],
  [
     "cat2",
     {
        "name":"d1",
        "id":"9",
        "name_arab":"d1",
        "price":"300",
        "logo":"watch.jpg"
     }
      ]
   ]
   }

最佳回答:


        JSONObject mainObject = new JSONObject(str);

        JSONArray businessArray = mainObject.getJSONArray("business_cat");

        JSONArray businessArray1 = businessArray.getJSONArray(0);
        String cat_1 = businessArray1.getString(0);
        for(int i = 1;i<businessArray1.length()-1;i++){
            JSONObject detail = businessArray1.getJSONObject(i);
            String name = detail.getString("name");
            String id = detail.getString("id");
            String name_arab = detail.getString("name_arab");
            String price = detail.getString("price");
            String logo = detail.getString("logo");
        }


        JSONArray businessArray2 = businessArray.getJSONArray(1);
        String cat_2 = businessArray2.getString(0);
        JSONObject detail2 = businessArray2.getJSONObject(1);
        {
            String name = detail2.getString("name");
            String id = detail2.getString("id");
            String name_arab = detail2.getString("name_arab");
            String price = detail2.getString("price");
            String logo = detail2.getString("logo");
        }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved