---
title: "angular.fromJson"
author: "boyitech"
date: "2014-12-18T16:02:50Z"
tags: []
categories: ["Web前端"]
slug: "angular-fromjson"
draft: false
lastmod: "2014-12-18T16:02:50Z"
id: 2168399
url: "https://boyitech.iteye.com/blog/2168399"
---

angular.fromJson

描述:

把Json字符串转为对象

使用方法:

angular.fromJson(json);

参数详解:

Param Type Details json [string](https://docs.angularjs.org/)

JSON 字符串

返回值:

对象, 数组, 字符串 或者是一个数字

示例:

```
<!DOCTYPE HTML>
<html ng-app="exampleApp">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script src="angular.min.js"></script>
	<script>
	var exampleApp = angular.module('exampleApp',[]);
	
	exampleApp.controller('ShowController',['$scope', function($scope){
		
		
		$scope.fromJson = function() {
			var strUser = "{\"firstName\":\"fox\",\"lastName\":\"zhang\"}";
			
			var objUser = angular.fromJson(strUser);
			alert(objUser.firstName);
		}
		
	}]);
	

	</script>
</head>

<body>
<div id="test">博弈网络科技</div>

	<div>
		<div>
		angular.fromJson("{\"firstName\":\"fox\",\"lastName\":\"zhang\"}")
		</div>
		
		<div id="myCtrl" ng-controller="ShowController">

      <input type="button" id="btnToJson" ng-click="fromJson()" value="objUser.firstName" />
		 </div> 
		
	</div>
<hr>



</body>	
</html>
```