JSON.js 下载地址:http://download.csdn.net/detail/u010989191/9532369
里面的index.html即是下面的演示示例。供需要的朋友参考!

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="./json2.js"></script>
</head>
<body>
<script type="text/javascript">
    //注意此时的json 最外层没有[]
    var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';  
    var contact = JSON.parse(jsontext);
    //第一种取值方法
    with(contact)
    {
        alert(firstname+" "+surname+" "+phone);
    }  
    //第二种取值方式
    alert(contact['phone']);
    //第三种取值方式
    document.write(contact.surname + ", " + contact.firstname + ", "+ contact.phone);  

    var jsontext2 = '{"name":"王五","age":"18","work":{"name":"programer","salary":"3k"}}';
    var text2Obj = JSON.parse(jsontext2);
    with(text2Obj)
    {
        alert(name+" "+age+" "+work.name+" "+work.salary);
        with(work)
        {
            alert(name+" "+salary);
        }
    }
</script>
</body>
</html>

【注】注意与使用eval(“(“+json+”)”)处理时对json数据格式的要求。

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐