Home > Web Front-end > JS Tutorial > JSON serialization operation implemented by JS

JSON serialization operation implemented by JS

不言
Release: 2018-07-03 15:33:47
Original
1708 people have browsed it

This article mainly introduces the JSON serialization operation implemented by JS. It analyzes the related implementation methods and related precautions of the json serialization operation in the form of simple examples. The code is equipped with more detailed comments for easy understanding. Friends in need can refer to it. Next

The example in this article describes the JSON serialization operation implemented by JS. Share it with everyone for your reference, the details are as follows:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>JOSN对象</title>
  <script>
    var book={
      title:"php 最佳实践",
      authors:[&#39;jack&#39;],
      edition:3,
      year:2017
    };
    //JSON.stringify()有三个参数分别是json对象,过滤器(可以是数组or函数),是否在josn字符串中保留缩进(这个参数意义不大可忽略)
    var jsonText=JSON.stringify(book);
    var jsonText1=JSON.stringify(book,["title","year"]);
    var jsonText2=JSON.stringify(book,function (key,value) {
      switch (key){
        case "title":
          return "java 从入门到精通";
        case "year":
          return 2018;
        case "edition":
          return undefined;
        default:
          return value;
      }
    });
    console.log(jsonText);//{"title":"php 最佳实践","authors":["jack"],"edition":3,"year":2017}
    console.log(jsonText1);//{"title":"php 最佳实践","year":2017}
    console.log(jsonText2);//{"title":"java 从入门到精通","authors":["jack"],"year":2018}
    //JSON.parse(arg1,arg2) 可以接受2个参数一个是json字符串,一个是过滤函数
    var jsonObject=JSON.parse(jsonText);
    var jsonObject1=JSON.parse(jsonText,function(key,value){
      if(key==="year"){
        return 2019
      }else if(key==="title"){
        return "javascript 从入门到精通";
      }else{
        return value;
      }
    });
    console.log(jsonObject);//Object {title: "php 最佳实践", authors: Array(1), edition: 3, year: 2017}
    console.log(jsonObject1);//Object {title: "javascript 从入门到精通", authors: Array(1), edition: 3, year: 2019}
  </script>
</head>
<body>
</body>
</html>
Copy after login

Running results:

The above is this article The entire content, I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

JQuery prevents event bubbling instance analysis

Introduction to the effect of JavaScript html5 canvas drawing villain

The above is the detailed content of JSON serialization operation implemented by JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template