最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON.parse() not working on jQuery data object - Stack Overflow

programmeradmin2浏览0评论

I get a JSON file via jQuery $.getJSON(url, function(data) { ... and want to parse it with either

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

This is the valid JSON file I'm using:

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

I'm not too familiar with Javascript. What am I doing wrong?

If I use a simple JSON array (= just the content of locations) I get valid data with $.each. Do I have to do something with data before I can use JSON.parse on it?

I get a JSON file via jQuery $.getJSON(url, function(data) { ... and want to parse it with either

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

This is the valid JSON file I'm using:

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

I'm not too familiar with Javascript. What am I doing wrong?

If I use a simple JSON array (= just the content of locations) I get valid data with $.each. Do I have to do something with data before I can use JSON.parse on it?

Share Improve this question edited Dec 8, 2015 at 9:24 Denys Séguret 383k90 gold badges811 silver badges776 bronze badges asked Feb 14, 2013 at 15:36 bolerobolero 781 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

The problem is that the name of the function is slightly misleading : it doesn't give you JSON but already a parsed object. What it does is fetch some JSON and parse it for you.

data is a plain javascript object, you don't need to parse it.

$.getJSON will parse the data for you - you don't need to parse it manually after the fact.

发布评论

评论列表(0)

  1. 暂无评论