toJson:将对象或者数组 序列化 为json格式的字符串
fromJson:相反的过程
1 <!DOCTYPE html> 2 <html ng-app="myApp"> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 <script src="js/angular.js"></script> 7 </head> 8 <body> 9 10 <div ng-controller="myCtrl"> 11 12 </div> 13 14 <script> 15 var app = angular.module('myApp', ['ng']); 16 17 app.controller('myCtrl', function ($scope) { 18 19 //反序列化 20 var jsonStr = '{"name":"zhangsan","age":20}'; 21 var result = angular.fromJson(jsonStr); 22 console.log(result); console.log(resultStr); 23 //Object age: 20name: "zhangsan"__proto__: Object 24 25 26 //序列化:将对象或者数组 序列化 为json格式的字符串 方便和服务器端的交互 27 var resultStr = angular.toJson(result); 28 // {"name":"zhangsan","age":20} 29 30 // angular.lowercase/uppercase() 31 32 // 遍历 33 var list = [100,200,300]; 34 angular.forEach(list, function (value,key) { 35 console.log(key,value); 36 }) 37 38 }); 39 </script> 40 </body> 41 </html>