JavaScript -- JSON.parse 函数 和 JSON.stringify 函数

JavaScript -- JSON.parse 函数 和 JSON.stringify 函数

1. JSON.parse 函数: 使用 JSON.parse 可将 JSON 字符串转换成对象。

<!doctype html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script type="text/javascript">
        var jsontext = '{"Name":"xiaohuzi","Age":"26","Email":"[email protected]","Phone":"123456789"}';
        var p = JSON.parse(jsontext);
        alert(p.Name + ", " + p.Age+", "+p.Email+", "+p.Phone);
  </script>
 </head>
 <body>

 运行结果:

2. JSON.stringify 函数:  可将 JavaScript 对象转换为Json表示字符串。

<!doctype html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script type="text/javascript">
        var Person = new Object();
        Person.Name = "XiaoHuzi";
        Person.Age = 26;
        Person.Email="[email protected]";

        var jsonText = JSON.stringify(Person);

        alert(jsonText);
  </script>
 </head>
 <body>

运行结果:

猜你喜欢

转载自www.cnblogs.com/ChengWenHao/p/javascriptPart15.html