• JSON相关


    //转换

    一个JSON数据类型的字付串,然后在JavaScript脚本中用eval()函数改变成JSON对象,就可以直接访问了。感觉这样比较方便,也更灵活一点。要注意的是要在返回的字符串两端加一对小括号

    var json1 = web_TreeView_TreeDemo.GetJson().value;

        json1 = eval('(' + json1 + ')');

    查看复制到剪切板打印

    1. function showJSON()      
    2. {          
    3. var user =           
    4.      {           
    5. "username":"andy",          
    6. "age":20,          
    7. "info": { "tel": "123456", "cellphone": "98765"},          
    8. "address":          
    9.              [          
    10.                  {"city":"beijing","postcode":"222333"},          
    11.                  {"city":"newyork","postcode":"555666"}          
    12.              ]          
    13.      }          
    14.      alert(user.username);          
    15.      alert(user.age);          
    16.      alert(user.info.cellphone);          
    17.      alert(user.address[0].city);          
    18.      alert(user.address[0].postcode);        
    19.      user.username = "Tom";          
    20.      alert(user.username);          
    21. }  
    function showJSON()   
     {       
        var user =        
        {        
            "username":"andy",       
            "age":20,       
            "info": { "tel": "123456", "cellphone": "98765"},       
            "address":       
                [       
                    {"city":"beijing","postcode":"222333"},       
                    {"city":"newyork","postcode":"555666"}       
                ]       
        }       
               
        alert(user.username);       
        alert(user.age);       
        alert(user.info.cellphone);       
        alert(user.address[0].city);       
        alert(user.address[0].postcode);     
        user.username = "Tom";       
        alert(user.username);       
         
    }

    将字符串转 -> json 对象:

    查看复制到剪切板打印

    1. //使用eval函数将字符串转化成json对象          
    2. function myEval()       
    3.     {             
    4. var str = '{ "name": "Violet", "occupation": "character" }';             
    5. var obj = eval('(' + str + ')');            
    6.     alert(obj.toJSONString());             
    7.     }    
    //使用eval函数将字符串转化成json对象          
       function myEval()    
       {          
       var str = '{ "name": "Violet", "occupation": "character" }';          
       var obj = eval('(' + str + ')');         
         
       alert(obj.toJSONString());          
       }

    查看复制到剪切板打印

    1. //使用 json.js 中的parseJSON方法   
    2. function myEval2() {             
    3. var str = '{ "name": "Violet", "occupation": "character" }';             
    4. var obj = str.parseJSON();             
    5.     alert(obj.toJSONString());    
    //使用 json.js 中的parseJSON方法   
       function myEval2() {          
       var str = '{ "name": "Violet", "occupation": "character" }';          
       var obj = str.parseJSON();          
       alert(obj.toJSONString());

    将 json 对象 -> 字符串转 :

    查看复制到剪切板打印

    1. function showCar() {          
    2. var carr = new Car("Dodge", "Coronet R/T", 1968, "yellow");          
    3.      alert(carr.toJSONString());          
    4. }          
    5. function Car(make, model, year, color)        {          
    6. this.make   =   make;          
    7. this.model   =   model;          
    8. this.year   =   year;          
    9. this.color   =   color;          
    10. }     
    function showCar() {       
        var carr = new Car("Dodge", "Coronet R/T", 1968, "yellow");       
        alert(carr.toJSONString());       
    }       
          
    function Car(make, model, year, color)       {       
         this.make  =  make;       
         this.model  =  model;       
         this.year  =  year;       
         this.color  =  color;       
    }

    查看复制到剪切板打印

    1. function test()      
    2.    {       
    3. var myJSONtext =       
    4.          {         
    5. "bindings":       
    6.              [         
    7.                  {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},         
    8.                  {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},         
    9.                  {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}         
    10.              ]         
    11.          };         
    12. var myObject = eval(myJSONtext);         
    13.          alert("对象长度:"+myObject.bindings.length);         
    14. for(var i=0;i<myObject.bindings.length;i++){         
    15.              alert(myObject.bindings[i].method);         
    16.          }        
    17.    }
  • 相关阅读:
    你看,那个人好像一条狗哎
    我竟然被抓去做了比特币挖矿工
    聊聊JAVA中 String类为什么不可变
    三分钟深入TT猫之故障转移
    shell实现两个数的相加
    shell截取字符串的方法
    Vi命令:如何删除全部内容?
    bash中不可以用字符串做数组下标
    awk打印出当前行的上一行
    awk同时处理多个文件
  • 原文地址:https://www.cnblogs.com/xyicheng/p/1660381.html
Copyright © 2020-2023  润新知