没有解析JSON数据?

时间:2012-01-11 00:28:38

标签: php javascript json

我遵循了关于如何使用JSON的建议here

使用后

$resultAml =  json_encode($resultArray);
在PHP中

我在客户端得到了这个:

[{"id":"1","0":"1","title":"Facebook","1":"Facebook","url":"http://facebook.com","2":"http://facebook.com","domain":"facebook.com","3":"facebook.com","tag":"","4":""},{"id":"1","0":"1","title":"Ideeli","1":"Ideeli","url":"http://www.ideeli.com","2":"http://www.ideeli.com","domain":"ideeli.com","3":"ideeli.com","tag":"web","4":"web"},{"id":"1","0":"1","title":"Kikin","1":"Kikin","url":"http://www.kikin.com","2":"http://www.kikin.com","domain":"kikin.com","3":"kikin.com","tag":"web","4":"web"},{"id":"1","0":"1","title":"Lot18","1":"Lot18","url":"http://www.lot18.com","2":"http://www.lot18.com","domain":"lot18.com","3":"lot18.com","tag":"web","4":"web"},{"id":"1","0":"1","title":"Quora","1":"Quora","url":"http://quora.com","2":"http://quora.com","domain":"quora.com","3":"quora.com","tag":"","4":""},{"id":"1","0":"1","title":"Twitter","1":"Twitter","url":"http://twitter.com","2":"http://twitter.com","domain":"twitter.com","3":"twitter.com","tag":"","4":""}] 

根据json.org

,它看起来像是有效的JSON

Howwever,

当我使用

在客户端解析它时
JSON.parse

我得到了这个结果:

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

看起来像表中每个条目的一个[object Object]。

什么是对象?

我在哪里出错?或者如果不是如何获得结果中的数据?

3 个答案:

答案 0 :(得分:5)

你有数据,你只是误解了它是如何显示的。任何JavaScript Object的默认字符串表示形式(JSON {...}值都是[object Object])。如果您尝试访问对象的属性,您将看到您期望的数据存在:

var input = '[{"id":"1","0":"1","title":"Facebook","1":"Facebook","url":"http://facebook.com","2":"http://facebook.com","domain":"facebook.com","3":"facebook.com","tag":"","4":""},{"id":"1","0":"1","title":"Ideeli","1":"Ideeli","url":"http://www.ideeli.com","2":"http://www.ideeli.com","domain":"ideeli.com","3":"ideeli.com","tag":"web","4":"web"},{"id":"1","0":"1","title":"Kikin","1":"Kikin","url":"http://www.kikin.com","2":"http://www.kikin.com","domain":"kikin.com","3":"kikin.com","tag":"web","4":"web"},{"id":"1","0":"1","title":"Lot18","1":"Lot18","url":"http://www.lot18.com","2":"http://www.lot18.com","domain":"lot18.com","3":"lot18.com","tag":"web","4":"web"},{"id":"1","0":"1","title":"Quora","1":"Quora","url":"http://quora.com","2":"http://quora.com","domain":"quora.com","3":"quora.com","tag":"","4":""},{"id":"1","0":"1","title":"Twitter","1":"Twitter","url":"http://twitter.com","2":"http://twitter.com","domain":"twitter.com","3":"twitter.com","tag":"","4":""}]';
var data = JSON.parse(input);
alert(data[0].title); // displays "Facebook"

答案 1 :(得分:1)

一切都是正确的。

打印对象属性没有特殊支持; { a: 42 }.toString()会返回[object Object]

要查看实际数据,请致电console.log(something) 这将在控制台中显示实际的对象结构。

答案 2 :(得分:1)

让我们来看看你的json解析数据。

[
 {
  "id":"1",
  "0":"1",
  "title":"Facebook",
  "1":"Facebook",
  "url":"http://facebook.com",
  "2":"http://facebook.com",
  "domain":"facebook.com",
  "3":"facebook.com",
  "tag":"",
  "4":""
},
{
  "id":"1",
  "0":"1",
  "title":"Ideeli",
  "1":"Ideeli",
  "url":"http://www.ideeli.com",
  "2":"http://www.ideeli.com",
  "domain":"ideeli.com",
  "3":"ideeli.com",
  "tag":"web",
  "4":"web"
},
{
  "id":"1",
  "0":"1",
  "title":"Kikin",
  "1":"Kikin",
  "url":"http://www.kikin.com",
  "2":"http://www.kikin.com",
  "domain":"kikin.com",
  "3":"kikin.com",
  "tag":"web",
  "4":"web"
},
{
  "id":"1",
  "0":"1",
  "title":"Lot18",
  "1":"Lot18",
  "url":"http://www.lot18.com",
  "2":"http://www.lot18.com",
  "domain":"lot18.com",
  "3":"lot18.com",
  "tag":"web",
  "4":"web"
},
{
  "id":"1",
  "0":"1",
  "title":"Quora",
  "1":"Quora",
  "url":"http://quora.com",
  "2":"http://quora.com",
  "domain":"quora.com",
  "3":"quora.com",
  "tag":"",
  "4":""
},
{
  "id":"1",
  "0":"1",
  "title":"Twitter",
  "1":"Twitter",
  "url":"http://twitter.com",
  "2":"http://twitter.com",
  "domain":"twitter.com",
  "3":"twitter.com",
  "tag":"",
  "4":""
}
]

您的数据中似乎有很多对象。 OBJECT是具有属性的数据集合。您的对象具有以下属性:

id, 0, title, 1, url, 2, domain, 3, tag, 4

如果与PHP模式进行比较,这就像一个多维数组。对于java和PHP,如果要处理所有条目,则必须迭代数据对象。你可以使用jQuery“each()”函数来完成它。

var data = 'yourJSONoutput';    
 $.each(data, function() {
     alert(this.title);
 });

这将警告json数据中所有对象的标题。