简体   繁体   中英

Filtering Json Using Javascript

I have the following JSON coming from a php ajax call and want to display some data to the users:

JSON

{"headers":{},"body":"{\"comuni\":[{\"datapresub\":\"08\/08\/2018\",\"datasub\":\"01\/02\/2018\",\"nomeComune\":\"ROMA\",\"provincia\":\"RM\"}]}","response":{"code":200,"message":"OK"},"cookies":[{"name":"bcf106ed7722e1b4a749630715ee3e66","value":"5338310d3fecaa76e2c9c583dfb02ddd","expires":null,"path":"\/","domain":"anpr-servizi-portale-anpr-portale-ocptest.ocpval.sogei.it","host_only":true}],"filename":null,"http_response":{"data":null,"headers":null,"status":null}}

This is my script:

success: function(data) {
                            console.log(data);
                            var jsonData = JSON.parse(data);  
                            if(jsonData[0].datasub != 0) {
                                $('.nome-comune').html(jsonData[0].nomeComune);
                                $('.provincia').html(jsonData[0].provincia);
                                $('.data-sub').html(jsonData[0].datasub);
                                resultsSub();
                            } else if(jsonData[0].datapresub != 0 && jsonData[0].datasub == 0) {
                                $('.nome-comune').html(jsonData[0].nomeComune);
                                $('.provincia').html(jsonData[0].provincia);
                                $('.data-presub').html(jsonData[0].datapresub);
                                resultsPresub();
                            } else if(jsonData[0].datapresub == 0 && jsonData[0].datasub == 0) {
                                $('.nome-comune').html(jsonData[0].nomeComune);
                                $('.provincia').html(jsonData[0].provincia);
                                noAnpr();
                            }     
                        }

I got the following error:

TypeError: jsonData[0] is undefined

Any idea to solve it?

Your object is not an array, you can't access it as jsonData[0] . Also your body JSON is inside a string, you have to parse it as well if you want to read from that.

Code:

 const jsonData = { "headers": {}, "body": "{\"comuni\":[{\"datapresub\":\"08\\/08\\/2018\",\"datasub\":\"01\\/02\\/2018\",\"nomeComune\":\"ROMA\",\"provincia\":\"RM\"}]}", "response": { "code": 200, "message": "OK" }, "cookies": [ { "name": "bcf106ed7722e1b4a749630715ee3e66", "value": "5338310d3fecaa76e2c9c583dfb02ddd", "expires": null, "path": "\\/", "domain": "anpr-servizi-portale-anpr-portale-ocptest.ocpval.sogei.it", "host_only": true } ], "filename": null, "http_response": { "data": null, "headers": null, "status": null } } const body = JSON.parse(jsonData.body) // Let's check if "body.comuni[0].datasub" exists before working with it. if (body && body.comuni && body.comuni[0] && body.comuni[0].datasub) { console.log(body.comuni[0].datasub) if(body.comuni[0].datasub.= 0) { // Add the rest of your logic here. } }

You can see by printing the parsed json, it is an Object and not an Array.

 var json = '{"headers":{},"body":{\"comuni\":[{\"datapresub\":\"08\/08\/2018\",\"datasub\":\"01\/02\/2018\",\"nomeComune\":\"ROMA\",\"provincia\":\"RM\"}]},"response":{"code":200,"message":"OK"},"cookies":[{"name":"bcf106ed7722e1b4a749630715ee3e66","value":"5338310d3fecaa76e2c9c583dfb02ddd","expires":null,"path":"\/","domain":"anpr-servizi-portale-anpr-portale-ocptest.ocpval.sogei.it","host_only":true}],"filename":null,"http_response":{"data":null,"headers":null,"status":null}}'; console.log(JSON.parse(json))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM