JSON: obj to str/str to obj; JS:str to arr/arr to str

Download json2.js
json2.js from https://raw.githubusercontent.com/douglascrockford/JSON-js/master/json2.js

<!DOCTYPE HTML>
<html>
<head>
  <script language="javascript" type="text/javascript" src="json2.js"></script>
  <script>
          function jsonObjToStr(data){
              return JSON.stringify(data);
          }
          function jsonStrToObj(data){
              return JSON.parse(data);
          }
          function arrToStr(data){
              return data.join("@@@");
          }
          function strToArr(data){
              return data.split("@@@");
          }
 
          var a = jsonObjToStr({name:"jack"});
          console.log(a);
 
          var a = jsonStrToObj('{"name":"jack"}');
          console.log(a);
 
          var a = arrToStr(['jack','robin']);
          console.log(a);
 
          var a = strToArr('jack@@@robin');
          console.log(a);
  </script>
</head>
<body>
</body>
</html>

Related posts:

Leave a Reply

Your email address will not be published.