SoFunction
Updated on 2025-04-09

Example of usage of toJson method

This article describes the usage of the toJson method. Share it for your reference, as follows:

AngularJS()Methods can parse a Json string into an object, or an object array:

<!DOCTYPE html>
<html ng-app="App">
<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="../js/"></script>
  <script type="text/javascript">
    ("App", [])
        .controller("parseController", function($scope)
        {
          $ = function()
          {
            var json = '{"name":"liSi", "password":"321"}';
            var jsonArr = '[{"name":"zhangSan", "password":"123"},{"name":"liSi", "password":"321"}]';
            var obj = (json);
            ();
            var objArr = (jsonArr);
            (objArr[0].name);
            (objArr[1].password);
          }
        });
  </script>
</head>
<body>
<div ng-controller="parseController">
  <button ng-click="parse()">Click me!</button>
</div>
</body>
</html>

The results of the console printing are as follows:

liSi
zhangSan
321

Accordingly, from Json to object, from object to Json:

var obj =
{
  name:"liSi", password:"321"
}
var str = (obj, true);
(str);

The printing results are as follows:

{
 "name": "liSi",
 "password": "321"
}

PS: Regarding json operation, here are some practical json online tools for your reference:

OnlineJSONCode verification, inspection, beautification and formatting tools:
http://tools./code/json

JSONOnline formatting tools:
http://tools./code/jsonformat

Online XML/JSONConvert each other tools:
http://tools./code/xmljson

jsonCode online formatting/beautification/compression/editing/converting tools:
http://tools./code/jsoncodeformat

OnlinejsonCompression/escaping tools:
http://tools./code/json_yasuo_trans

For more information about AngularJS, readers who are interested in view the topic of this site:Summary of AngularJS command operation skills》、《AngularJS Introduction and Advanced Tutorial"and"AngularJS MVC architecture summary

I hope this article will be helpful to everyone's AngularJS programming.