zoukankan      html  css  js  c++  java
  • js 中 json.stringfy()将对象、数组转换成字符串

    json.stringfy()将对象、数组转换成字符串

    //1
    var student = new Object(); 
    student.name = "Lanny"; 
    student.age = "25"; 
    student.location = "China"; 
    var json = JSON.stringify(student); 
    alert(json); 
    //alert(student);

    如图所示:

    假如,我们不要这个函数,而直接alert(student),结果如下:

    以下示例使用 JSON.parse 将 JSON 字符串转换成对象。
    
    var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}'; 
    var contact = JSON.parse(jsontext); 
    document.write(contact.surname + ", " + contact.firstname); 
     
    // Output: Aaberg, Jesper
    以下示例演示了如何使用 JSON.stringify 将数组转换成 JSON 字符串,然后使用 JSON.parse 将该字符串重新转换成数组。
    
    var arr = ["a", "b", "c"]; 
    var str = JSON.stringify(arr); 
    document.write(str); 
    document.write ("<br/>"); 
     
    var newArr = JSON.parse(str); 
     
    while (newArr.length > 0) { 
        document.write(newArr.pop() + "<br/>"); 
    } 
     
     
    // Output: 
    // ["a","b","c"] 
    // c 
    // b 
    // a

      来自于 :https://www.cnblogs.com/panmy/p/5925986.html

  • 相关阅读:
    window.open()参数列表
    感受教育,焦点访谈
    《迷墙》
    锻炼身体
    Great Fire Wall
    今天
    查询重复记录的SQL语句
    Oracle,SQL Server,Access万能数据库通用类!
    经典SQL语句大全
    C#编码规范
  • 原文地址:https://www.cnblogs.com/JonaLin/p/11352210.html
Copyright © 2011-2022 走看看