无法动态从 json 数组获取数据
Not able to fetch data from json array dynamically
>i 有 json 数组
JSON.stringify(ar)
当我习惯在警报中显示结果时,例如
alert(JSON.stringify(ar));
它将按原样显示。 警报中的输出很简单
[{"url":"link1","title":"title1"}]
但是当我用来将其内容传输到播放列表数组中时,例如
var playlist=[];
playlist=JSON.stringify(ar); alert (JSON.stringify(playlist[1].url));
并尝试显示其结果,它给了我错误并给了我未定义
请帮我整理一下。
你需要处理对象本身。 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"
在此之后
var playlist=[];
playlist=JSON.stringify(ar)
播放列表包含字符串,因此如果要提取URL,则需要再次解析该JSON
。alert(JSON.parse(playlist)[1].url);
但是如果你放[1]
那么数组需要有两个元素:
[{"url":"link1","title":"title1"},{"url":"link1","title":"title1"}]
相关文章:
- 从json子数组获取数据
- 从javascript数组获取php的日期时间
- 从表中的 JSON 数组获取数据
- 每个表的2D数组获取坐标JTSL,JAVASCRIPT
- javascript从元素到数组获取文本
- 如何从数组获取链接以在youtube Iframe API中播放
- JSON 编码多维数组获取值
- 如何从两个 JSON 数组获取增量
- 通过单击按钮数组获取隐藏 html 元素值的当前数组
- 在 JavaScript 中从内部数组获取数据
- 从数组获取非重复索引在 0 时失败
- JavaScript 从对象中的数组获取数组
- 如何通过 ajax 将 php 数组作为 javascript 数组获取
- 快速从 2D 数组获取索引,无需迭代 jquery
- 从 Nodejs socketio 数组获取所有套接字 id
- 无法动态从 json 数组获取数据
- 如何在javascript中使用数组获取和设置html标签的值
- jQuery对象数组获取一个项作为jQuery对象
- 如何在不向输出数组获取空值的情况下拆分字符串
- 从输入到数组获取数据