How to fetch Data From JSON

前端 未结 6 1166
予麋鹿
予麋鹿 2021-01-27 15:01

I have following data from JSON

{
        \"MenuName\": \"starter dish\",
        \"SubMenu\": [
            \"pizza dish1\",
            \"pizza dish2\"
                


        
6条回答
  •  耶瑟儿~
    2021-01-27 15:28

    You can retrieve array values and store it in string[] and use them as per your need. i.e., as follows..

    try {
                JSONObject jObject = new JSONObject(jsonString);
                JSONArray jSubmenu = jObject.getJSONArray("SubMenu");
                subMenu = new String[jSubmenu.length()];
                for (int i = 0; i < subMenu.length; i++) {
                    subMenu[i] = jSubmenu.getString(i);
                }
                JSONArray jPrice = jObject.getJSONArray("Price");
                price = new String[jPrice.length()];
                for (int i = 0; i < price.length; i++) {
                    price[i] = jPrice.getString(i);
                }
    
            } catch (Exception e) {
                // TODO: handle exception
            }
    

提交回复
热议问题