簡體   English   中英

使用 Javascript 過濾 Json

[英]Filtering Json Using Javascript

我有以下 JSON 來自 php ajax 調用並想向用戶顯示一些數據:

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

這是我的腳本:

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();
                            }     
                        }

我收到以下錯誤:

TypeError: jsonData[0] is undefined

有什么辦法解決嗎?

您的 object 不是數組,您無法將其作為jsonData[0]訪問。 你的body JSON 也在一個字符串中,如果你想從中讀取,你也必須解析它。

代碼:

 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. } }

通過打印解析的 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