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

  • 相关阅读:
    Two Sum II
    Subarray Sum
    Intersection of Two Arrays
    Reorder List
    Convert Sorted List to Binary Search Tree
    Remove Duplicates from Sorted List II
    Partition List
    Linked List Cycle II
    Sort List
    struts2结果跳转和参数获取
  • 原文地址:https://www.cnblogs.com/JonaLin/p/11352210.html
Copyright © 2011-2022 走看看