javaScript--5 javascript json字符串转换

 

 

<!DOCTYPE html>
<html>

<head>
  <title>JavaScript对象转JSON字符串</title>
</head>

<body>
  <script>
    var jsonObj = {
      "userId": "admin",
      "password": "123456",
      "code": "428m",
      "sessionId": "64F3124042EC11EDB8F1974F809A2BE6",
      "userinfo": [
        {
          "username": "dddddd",
          "sex": "男",
          "add": "深圳南山"
        },
        {
          "member": true,
          "level": 5
        }
      ]
    }
    //json串
    console.log(jsonObj)
    //获取姓名
    console.log(jsonObj.userinfo[0].username);
    //json对象转String
    var userString = JSON.stringify(jsonObj);
    console.log(userString);
    //string转JSON
    var userJson = JSON.parse(userString);
    console.log(userJson);
  </script>
</body>

</html>

 

posted @ 2022-10-03 15:40  怪圣卡杰  阅读(23)  评论(0编辑  收藏  举报