如何输出这个json呢?
{posts:[
{
id:1,
title:"帖子1",
content:"内容",
posterId:13
},
{
id:2,
title:"帖子2",
content:"内容",
posterId:13
}
]};a = eval("("+posts+")")
document.write(a[2]['title']);
请问如何输出id=2的title值呢?我这样不行
{posts:[
{
id:1,
title:"帖子1",
content:"内容",
posterId:13
},
{
id:2,
title:"帖子2",
content:"内容",
posterId:13
}
]};a = eval("("+posts+")")
document.write(a[2]['title']);
请问如何输出id=2的title值呢?我这样不行
关于转换JSON字符串可以使用JSON2.js,网上有下载使用JSON.parse(json字符串)这个方法可以把字符串转成JS对象
如
var str = {posts:[
{
id:1,
title:"帖子1",
content:"内容",
posterId:13
},
{
id:2,
title:"帖子2",
content:"内容",
posterId:13
}
]};
var obj = JSON.parse(str);alert(obj[1]['title']);就可以了
上面写错了最后应该是alert(obj['post'][1]['title']);
<script type="text/javascript">
<!--
var parseJSON = {posts:[
{
id:1,
title:"帖子1",
content:"内容",
posterId:13
},
{
id:2,
title:"帖子2",
content:"内容",
posterId:13
}
]};document.write(parseJSON.posts[1].title);//-->
</script>