• json


    json1

    <html>
    <body>
        <h2>在 JavaScript 中创建 JSON 对象</h2>
        <p>
            Name: <span id="jname"></span><br /> 
            Age: <span id="jage"></span><br />
            Address: <span id="jstreet"></span><br /> 
            Phone: <span id="jphone"></span><br />
        </p>
        <script type="text/javascript">
            var JSONObject = {
                "name" : "Bill Gates",
                "street" : "Fifth Avenue New York 666",
                "age" : 56,
                "phone" : "555 1234567"
            };
            document.getElementById("jname").innerHTML = JSONObject.name
            document.getElementById("jage").innerHTML = JSONObject.age
            document.getElementById("jstreet").innerHTML = JSONObject.street
            document.getElementById("jphone").innerHTML = JSONObject.phone
        </script>
    </body>
    </html>

    json2

    <html>
    <body>
        <h2>在 JavaScript 中创建 JSON 对象</h2>
        <p>
            Name: <span id="jname"></span><br /> 
            Age: <span id="jage"></span><br />
            Sex: <span id="jsex"></span><br />
            Address: <span id="jstreet"></span><br /> 
            Phone: <span id="jphone"></span><br />
        </p>
        <script type="text/javascript">
            var JSONObject = {
                "name" : "Bill Gates",
                "street" : "Fifth Avenue New York 666",
                "age" : 56,
                "sex":"",
                "phone" : "555 1234567"
            };
            document.getElementById("jname").innerHTML = JSONObject.name
            document.getElementById("jage").innerHTML = JSONObject.age
            document.getElementById("jsex").innerHTML = JSONObject.sex
            document.getElementById("jstreet").innerHTML = JSONObject.street
            document.getElementById("jphone").innerHTML = JSONObject.phone
        </script>
    </body>
    </html>

    json3

    <html>
    <body>
        <h2>通过 JSON 字符串来创建对象</h2>
        <p>
            First Name: <span id="fname"></span>
        </p>
    
        <script type="text/javascript">
            var employees = [ {
                "firstName" : "Bill",
                "lastName" : "Gates"
            }, {
                "firstName" : "George",
                "lastName" : "Bush"
            }, {
                "firstName" : "Thomas",
                "lastName" : "Carter"
            } ];
            employees[1].firstName = "Chen";
            document.getElementById("fname").innerHTML = employees[1].firstName;
        </script>
    </body>
    </html>

    json4

    <html>
    <body>
        <h3>通过 JSON 字符串来创建对象</h3>
        <p>
                First Name: <span id="fname"></span><br /> 
                Last Name: <span  id="lname"></span><br />
            </p>
            <script type="text/javascript">
                var txt = '{"employees":['
                        + '{"firstName":"Bill","lastName":"Gates" },'
                        + '{"firstName":"George","lastName":"Bush" },'
                        + '{"firstName":"Thomas","lastName":"Carter" }]}';
    
                var obj = eval("(" + txt + ")");
                document.getElementById("fname").innerHTML = obj.employees[1].firstName
                document.getElementById("lname").innerHTML = obj.employees[1].lastName
            </script>
    </body>
    </html>

  • 相关阅读:
    js json string 互转
    更新内置flash方法[转]
    CSS设置滚动条样式[转]
    ArcGIS JavaScript API本地部署离线开发环境[转]
    正则匹配整数和小数
    把数字字符串转换为日期
    获取滚动条高度的兼容问题
    angular实现多个div的展开和折叠
    IE浏览器overflow:srcoll兼容问题记录
    input验证,光标左右移动问题
  • 原文地址:https://www.cnblogs.com/jianfengyun/p/3792174.html
Copyright © 2020-2023  润新知