开发者

jQuery parseJSON returns double data

I have a PHP file which returns this:

$data = pg_fetch_array($result);
echo json_encode($data);

And in the .js code i do this:

var jsonObj = $.parseJSON(text);
console.dir(jsonObj);

And my problem is I get this "double" data:

{"0":"3","id":"3","1":"test","type":"test","2":"google","name":"google", "3":"http://开发者_运维技巧www.google.com","url":"http://www.google.com"}

Any ideas on how to retrieve it without those indexes (0,1,2...)?


The problem is not in parseJSON method but in pg_fetch_array. And it is the expected behaviour.

PHP Documentation says that pg_fetch_array by default "indexed numerically (beginning with 0) or associatively (indexed by field name)" so you can access values either by index or by name.

To change this behaviour, use either

$data = pg_fetch_array($result, 0, PGSQL_ASSOC); //Keys are names only
$data = pg_fetch_array($result, 0, PGSQL_NUM); //Keys are indexes only


I think you dont need to parse that using json, if you are using jquery ajax method then just mention dataTye: json, then you can directly access that object.


Try pg_fetch_array($result, NULL, PGSQL_ASSOC);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜