如何在JavaScript中JSON解码数组元素?

如何在JavaScript中JSON解码数组元素?

问题描述:

我有一个JavaScript数组,其中包含一个URL。如果我尝试简单地将URL放在页面中(该数组位于涉及Yahoo! Maps API的项目中),它将显示URL应该是。

I have a JavaScript array that, among others, contains a URL. If I try to simply put the URL in the page (the array is in a project involving the Yahoo! Maps API) it shows the URL as it should be.

但是如果我尝试重定向或只是对链接数组元素执行警告,我得到:

But if I try to do a redirect or simply do an 'alert' on the link array element I get:


function(){return JSON .encode(this);}

function(){return JSON.encode(this);}

据我所知,这是因为浏览器在渲染时会执行JSON.encode页面,因此链接显示确定。我已经尝试了几种方法使其重定向(这是我想要对链接做的)正确(包括使用'eval')但没有运气。

As far as I see it this is because the browser does an JSON.encode when it renders the page, thus the link is displayed OK. I have tried several methods to make it redirect (that's what I want to do with the link) correctly (including the usage of 'eval') but with no luck.

在遵循一些建议之后我运行 eval('('+ jsonObject +')')但它仍然返回相同的输出。

After following some suggestions I've run eval('(' + jsonObject + ')') but it still returns the same output.

那怎么办呢?

var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );

请参阅jQuery API