繁体   English   中英

我的JSON有什么问题

[英]What is Wrong with my JSON

我正在使用jquery函数$ .getJson。 它正在发送我想要的数据,并且生成JSON的PHP脚本正在正常工作。 但是,我遇到的问题从这里发生。

在我的$ .getJSON代码中,我希望它仅将成功记录下来的结果记录下来,如果有错误则记录为错误...你猜到了...我遇到了错误...

这是我传递给它的JSON代码,它会产生错误,但我看不出它出了什么问题。

  {
"results":{
"city":[
    "metric":"visits",
    "dimension":"city",
    "data":[["London","5"],["Glasgow","3"],["(not set)","1"],["Aberdeen","1"],["Antalya","1"]]
    ],

"browser":[
    "metric":"visits",
    "dimension":"browser",
    "data":[["Internet Explorer","11"],["Chrome","9"],["Firefox","3"],["Safari","3"],["Android Browser","2"]]
    ],

"screenResolution":[
    "metric":"visits",
    "dimension":"screenResolution",
    "data":[["1280x800","5"],["1680x1050","4"],["1024x768","3"],["1366x768","3"],["1280x1024","2"]]
    ]
    }
    }

更新显示代码

这是我用来获取结果的代码

$.ajax({

//DEFINE URL being called by Ajax
url:'./getAnalytics.php',
dataType: "json",
data:{dateToSearch:dateToSearch},
success:function(resultsToView){

console.log(resultsToView);


},error:function(resultsToView){
console.log("Error: " + resultsToView);
}
});

控制台显示: resultsToView的结果

当我得到答案时,错误函数被调用而不是成功。 请帮忙。

您的JSON 无效的。 方括号表示数组 (列表),其中项没有键。 更改

[
  "metric":"visits",
  "dimension":"…",
  …
]

带有花括号的对象 (地图)语法:

{
  "metric":"visits",
  "dimension":"…",
  …
}

同样,错误回调具有签名Function(jqXHR xhr, String textStatus, String errorThrown) 您正在记录xhr对象的字符串化,这没有帮助。 更好地使用

…,
error: function(xhr, status, error) {
    console.log("Error: "+error+" ("+status+")", xhr);
}

您的JSON无效。 例如

"city":[

应该:

"city":{

[是用于数组。

您可以使用JSON验证程序来检查您的JSON是否良好,例如JSONLint

暂无
暂无

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

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