0

我有 json 数组

JSON.stringify(ar)

当我用来在警报中显示结果时

alert(JSON.stringify(ar));

它会按原样显示。警报中的输出很简单

[{"url":"link1","title":"title1"}]

但是当我用来将其内容传输到播放列表数组时,就像

var playlist=[];
playlist=JSON.stringify(ar); alert (JSON.stringify(playlist[1].url));

并尝试显示其结果,它给了我错误并给了我未定义

请帮我整理一下。

4

2 回答 2

0

在这之后

var playlist=[];

playlist=JSON.stringify(ar)

播放列表包含字符串,因此如果要提取 url,则需要再次解析该 JSON

alert(JSON.parse(playlist)[1].url);

但是如果你把[1]那么数组需要有两个元素:

[{"url":"link1","title":"title1"},{"url":"link1","title":"title1"}]
于 2013-10-02T17:27:11.120 回答
0

您需要处理对象本身。JSON.stringify当您输出对象或通过网络发送它们时,只需要以可读格式显示它们。

var ar = [{"url":"link1","title":"title1"}]

alert(ar); // equivalent to alert(ar.toString()), will show [object Object]
alert(JSON.stringify(ar)); // will show [{"url":"link1","title":"title1"}]
console.log(ar); // the proper way to do it, inspect the result in console

var playlist=[];

// then do either
playlist = playlist.concat(ar);
// or
playlist.push.apply(playlist, ar);
// or
playlist.push(ar[0]);
// or
playlist[0] = ar[0];
// or
playlist = ar;
// (which all do a little different things)
// but notice none of them used JSON.stringify!

// now you can
console.log(playlist)
alert(playlist[0].url); // shows link1 - this is what you want
alert(JSON.stringify(playlist[0].url)); // shows "link1"
于 2013-10-02T17:31:33.440 回答