简体   繁体   English

使用 Javascript 过滤 Json

[英]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 来自 php ajax 调用并想向用户显示一些数据:

JSON 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] .您的 object 不是数组,您无法将其作为jsonData[0]访问。 Also your body JSON is inside a string, you have to parse it as well if you want to read from that.你的body JSON 也在一个字符串中,如果你想从中读取,你也必须解析它。

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.通过打印解析的 json 可以看到,它是 Object 而不是数组。

 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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