Home > Software engineering >  JSON error , no value for when value exists
JSON error , no value for when value exists

Time:03-17

I have a JSON which looks like this:

{"count":23,"total_pages":3,"per_page":10,"current_page":1,"results":[{"category":{"name":"Electronics","icon":"https://www.freeiconspng.com/thumbs/iphone-x-pictures/apple-iphone-x-pictures-5.png","id":7},"name":"Xiaomi K20 Pro","details":"DISPLAY\tType\tSuper AMOLED, HDR10\r\nSize\t6.39 inches, 100.2 cm2 (~86.1% screen-to-body ratio)\r\nResolution\t1080 x 2340 pixels, 19.5:9 ratio (~403 ppi density)\r\nProtection\tCorning Gorilla Glass 5\r\nPLATFORM\tOS\tAndroid 9.0 (Pie), upgradable to Android 10, MIUI 12\r\nChipset\tQualcomm SM8150 Snapdragon 855 (7 nm)\r\nCPU\tOcta-core (1x2.84 GHz Kryo 485 & 3x2.42 GHz Kryo 485 & 4x1.78 GHz Kryo 485)\r\nGPU\tAdreno 640\r\nMEMORY\tCard slot\tNo\r\nInternal\t64GB 6GB RAM, 128GB 6GB RAM, 128GB 8GB RAM, 256GB 8GB RAM\r\n \tUFS 2.1\r\nMAIN CAMERA\tTriple\t48 MP, f/1.8, 26mm (wide), 1/2.0\", 0.8µm, PDAF, Laser AF\r\n8 MP, f/2.4, 53mm (telephoto), 1/4.0\", 1.12µm, PDAF, 2x optical zoom\r\n13 MP, f/2.4, 12mm (ultrawide), 1/3.1\", 1.12µm\r\nFeatures\tDual-LED flash, HDR, panorama\r\nVideo\t4K@30/60fps, 1080p@30/120/240fps, 1080p@960fps","size":"156.7 x 74.3 x 8.8 mm (6.17 x 2.93 x 0.35 in)","colour":"Red","price":400.0,"sold_count":0,"id":3},{"category":{"name":"Electronics","icon":"https://www.freeiconspng.com/thumbs/iphone-x-pictures/apple-iphone-x-pictures-5.png","id":7},"name":"Cubot KingKong MINI2 Rugged Phone 4","details":"We are Cubot Official Store & guarantee that all items are 100% original and manufacturer authorized.\r\nPlease read here carefully before purchasing, in order to protect your benefits.\r\nThe first thing after you receive the parcel, pls check the outside package condition as well as the inside goods , Try to take a video showing about you unpack the package. If there is any further problem, that will play an important role.\r\n\r\nBasic Information\t\r\n- Model: Cubot KINGKONG MINI2\r\n- Band: GSM 850/900/1800/1900MHz\r\n\r\nWCDMA  900/2100MHZ\r\n\r\nFDD  B1/3/7/8/19/20\r\n- Sim Card: Dual Nano SIM Card \r\n- Color: Orange-black  / Red-black\r\n- Battery: 3000mAh    \r\n- Battery type: undetachable  battery\r\n\r\nSystem\t\r\n- OS: Android10.0\r\n- CPU: MT6761 (Quad Core)\r\n- ROM: 32G\r\n- RAM: 3G\r\n- Card Extend:Support TF card up to 128GB extended\r\n\r\nScreen\t\r\n- Display Size:4.0Inch,QHD \r\n- Resolution: 540*1080(QHD )\r\n\r\nGeneral \t\r\n- Camera/Picture Resolution:Dual Cameras, front camera 8.0MP camera, back camera 13.0MP with 1A LED flashlight \t\r\n- Language:\"Japanese,Traditional/Simplified Chinese,Bahasa Indonesia, Bahasa Melayu, Catala, Cestina, Dansk, Deutsch,English, Espanol,Filipino,France, Hrvatski, Italiano,Magyar, Nederlands, Polski, Portugues, Romana, Slovenscina, Suomi, Svenska, Vietnamese, Turkish, Greek, Bulgarian, Russian, Serbian, Ukrainian, Hebrew, Arabic,  Hindi, Bengali, HindiThai, Korean,\"\r\n\r\n- GPS:\"Yes,built in. also support A-GPS\"\r\n- Sensor:\"Face ID ,Proximity, Accelerometer, Magnetic , gyroscope , Others(Light sensor) \"\r\n- Dimensions: 119*58*12.5mm\r\n- Net Weight(Including battery): 122.70g\r\n\r\nPackage:\r\n-Accessories: \r\n1x Cubot KingKong MINI2 smartphone\r\n1x USB Cable\r\n1 x Charger    5V1A\r\n1 x Screwdriver\r\n1 x User Manual\r\n\r\nProduct type: Smartphones","size":"- Display Size:4.0Inch,QHD  - Resolution: 540*1080","colour":"BLACK","price":1710.0,"sold_count":0,"id":2},{"category":{"name":"Home & Kitchen","icon":"https://static.wixstatic.com/media/2cd43b_dd8a9bb315c146789cfbd614e0673c2b~mv2.png/v1/fill/w_321,h_254,q_90/2cd43b_dd8a9bb315c146789cfbd614e0673c2b~mv2.png","id":4},"name":"Dopio Coffee","details":"Dopio Coffee Dopio Coffee Dopio Coffee Dopio Coffee Dopio Coffee Dopio Coffee Dopio Coffee Dopio Coffee Dopio Coffee Dopio Coffee Dopio Coffee Dopio Coffee","size":"LARGE (450 mm)","colour":"Dark Blue","price":980.0,"sold_count":0,"id":4},{"category":{"name":"Electronics","icon":"https://www.freeiconspng.com/thumbs/iphone-x-pictures/apple-iphone-x-pictures-5.png","id":7},"name":"Flex","details":"Detailed flex","size":"Big","colour":"Ha","price":560.0,"sold_count":0,"id":5},{"category":{"name":"Health & Beauty","icon":"https://www.pngarts.com/files/5/Makeup-Cosmetic-PNG-Transparent-Image.png","id":3},"name":"Health","details":"Detailed health","size":"Small","colour":"Blue","price":660.0,"sold_count":0,"id":6},{"category":{"name":"Home & Kitchen","icon":"https://static.wixstatic.com/media/2cd43b_dd8a9bb315c146789cfbd614e0673c2b~mv2.png/v1/fill/w_321,h_254,q_90/2cd43b_dd8a9bb315c146789cfbd614e0673c2b~mv2.png","id":4},"name":"Kitchen","details":"Detailed kitchen","size":"Kitchen","colour":"White","price":2500.0,"sold_count":0,"id":7},{"category":{"name":"Hobby, Sport, Traveling","icon":"https://pngimg.com/uploads/bicycle/bicycle_PNG5364.png","id":5},"name":"Another","details":"One","size":"500 mm","colour":"Killer Red","price":350.0,"sold_count":0,"id":8},{"category":{"name":"Electronics","icon":"https://www.freeiconspng.com/thumbs/iphone-x-pictures/apple-iphone-x-pictures-5.png","id":7},"name":"Eights","details":"Two connected circles","size":"8","colour":"Black","price":888.0,"sold_count":0,"id":9},{"category":{"name":"Electronics","icon":"https://www.freeiconspng.com/thumbs/iphone-x-pictures/apple-iphone-x-pictures-5.png","id":7},"name":"Apple Iphone 11","details":"Iphone 10 1","size":"5.6","colour":"White","price":350.0,"sold_count":0,"id":10},{"category":{"name":"Home & Kitchen","icon":"https://static.wixstatic.com/media/2cd43b_dd8a9bb315c146789cfbd614e0673c2b~mv2.png/v1/fill/w_321,h_254,q_90/2cd43b_dd8a9bb315c146789cfbd614e0673c2b~mv2.png","id":4},"name":"House","details":"Big house","size":"Big","colour":"White","price":15000.0,"sold_count":0,"id":11}]}

When i try to get "icon" I get an error No value for icon, but icon value exists, how i can resolve this problem? Parsing code :

private void jsonParse() {



    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("results");

                        for (int i = 0; i < jsonArray.length(); i  ) {
                            JSONObject employee = jsonArray.getJSONObject(i);
                            String title = employee.getString("name");
                            String details = employee.getString("details");
                            String iconurl = employee.getString("icon");
                           //with optString("icon); also doesn't work
                           // String iconurl = employee.optString("icon");
                          
                            String price = employee.getString("price");
                            System.out.println("ooooooooooooooooooooooooooooooooo" iconurl);
                            productList.add(new Product(title,iconurl, details, price));
                            buildRecyclerView();

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void one rrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

    mQueue.add(request);
}

CodePudding user response:

The icon property is not on the "employee" object, but on the category object inside of your result.

JSONObject category = employee.getJSONObject("category")
String iconurl = category.getString("icon")
  • Related