如何输出这个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值呢?我这样不行

解决方案 »

  1.   


    关于转换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']);就可以了
      

  2.   


    上面写错了最后应该是alert(obj['post'][1]['title']);
      

  3.   


    <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>
      

  4.   

    Json就是对象。用对象访问的方式。