简体   繁体   English

JSON.parse()不能处理jQuery数据对象

[英]JSON.parse() not working on jQuery data object

I get a JSON file via jQuery $.getJSON(url, function(data) { ... and want to parse it with either 我通过jQuery $.getJSON(url, function(data) { ...想要获取一个JSON文件$.getJSON(url, function(data) {并想用

var obj = JSON.parse(data);

or 要么

var obj = jQuery.parseJSON(data);

The first line gives me "syntax error" (using IE8, should support JSON.parse ), the second gives me "'center' is null or not an object". 第一行给我“语法错误”(使用IE8,应支持JSON.parse ),第二行给我“中心”为空或不是对象”。

This is the valid JSON file I'm using: 这是我正在使用的有效JSON文件:

{
"center":{"lat":"51.99637","lon":"13.07520"},
"locations":
[
    { "name":"a string","info":"another string" },
    ... some more here ...
]
}

I'm not too familiar with Javascript. 我对Javascript不太熟悉。 What am I doing wrong? 我究竟做错了什么?

If I use a simple JSON array (= just the content of locations) I get valid data with $.each . 如果我使用简单的JSON数组(=仅是位置的内容),则可以通过$.each获得有效数据。 Do I have to do something with data before I can use JSON.parse on it? 在可以使用JSON.parse之前,我必须对数据做些什么吗?

The problem is that the name of the function is slightly misleading : it doesn't give you JSON but already a parsed object. 问题在于该函数的名称有点误导性:它不提供JSON但已经提供了已解析的对象。 What it does is fetch some JSON and parse it for you. 它的作用是获取一些JSON并为您解析。

data is a plain javascript object, you don't need to parse it. data是一个普通的javascript对象,你不需要解析它。

$ .getJSON将为您解析数据 - 您不需要在事后解析它。

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

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