2017-04-10 69 views
2

在AngularJs,以下是工作 -

$scope.product= {items:[{itemname:'apple',itemdesc:'fruit'}]} 

但我想从下面的JSON字符串得到$scope.product -

var strJson = "{items:[{itemname:'apple',itemdesc:'fruit'}]}"; 
$scope.product=strJson; 

不工作。我所有的是一个JSON字符串,我需要分配到$scope.product。我已使用JSON.parse()JSON.toJson(),但它们不起作用。 我不知道我做错了什么

下面是我的确切代码:

$http.get('/getItems').success(function(data) { 
    var jsondata="{\items\:"; 
    jsondata+=JSON.stringify(data); 
    jsondata+="}"; 
    jsondata=jsondata.replace(/"itemname"/g, 'itemname'); 
    jsondata=jsondata.replace(/"itemdesc"/g, 'itemdesc'); 

     // WORKING CODE 
    $scope.product = {items:[{itemname:"apple",itemdesc:"fruit"}]}; 

     // NOT WORKING CODE 
    var jsonObj = jsondata; 
    $scope.product = jsonObj; 
}); 

回答

1

这是问题所在。显然,如果您使用JSON.parse将JSON字符串转换为JSON,那么您的属性名称应该用双引号而不是单引号括起来。

angular.module("app",[]) 
 
.controller("ctrl",function($scope){ 
 

 
$scope.product= '{"items":[{"itemname":"apple","itemdesc":"fruit"}]}' 
 
    $scope.product = JSON.parse($scope.product); 
 
    
 
console.log($scope.product) 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="ctrl"> 
 
    
 
</div>

+0

我刚编辑答案。希望downvoter将审查这一 –

+0

Myexact问题是这样的:$ http.get( '/ getItems')成功(功能(数据){ \t VAR jsondata = “{\项目\”。 \t jsondata + = JSON.stringify (数据); \t jsondata + = “}”; \t jsondata = jsondata.replace(/ “ITEMNAME”/ G 'ITEMNAME'); \t jsondata = jsondata。替换(/“itemdesc”/ g,'itemdesc'); \t \t // \t工作代码 \t $ scope.product = {项目:[{ITEMNAME: “苹果”,itemdesc: “水果”}]}; \t \t \t \t \t // \t不工作CODE \t变种jsonObj = jsondata; \t $ scope.product = jsonObj; }); –

0

您可以使用JSON.parse,发现下面

var strJson = JSON.parse("{items:[{itemname:'apple',itemdesc:'fruit'}]}")答案;

0

我会建议使用角度的方式来CONVER JSON反对:

$scope.product= "{items:[{itemname:'apple',itemdesc:'fruit'}]}"; 
$scope.product = angular.fromJson($scope.product); 

和对象到JSON如下:

var strJson = angular.toJson($scope.product); 
+0

请检查编辑问题 –

4

JSON standard预计值有双引号周围。所以你的JSON字符串不可解析JSON。

var mystr = '{"items": [{"itemname": "apple","itemdesc": "fruit"}]}'; 
 
console.log(JSON.parse(mystr)) 
 
console.log(typeof JSON.parse(mystr))

+0

这是我的确切代码: –

+0

$ http.get( '/ getItems')成功(功能(数据){ \t VAR jsondata = “{\项目\”。 \t jsondata + = JSON .stringify(数据); \t jsondata + = “}”; \t jsondata = jsondata.replace(/ “ITEMNAME”/ G 'ITEMNAME'); \t jsondata = jsondata.replace(/ “itemdesc”/ G' itemdesc'); \t \t // \t工作代码 \t $ scope.product = {项目:[{ITEMNAME: “苹果”,itemdesc: “水果”}]}; \t \t \t \t \t \t // NOT WORKING CODE \t var jsonObj = jsondata; \t $ scope.product = jsonObj; }); –

0

我认为您的字符串格式不正确的,它应该是这样的。

var str = '{"items":[{"itemname":"apple","itemdesc":"fruit"}]}'; 
$scope.product = JSON.parse(str) 
0

请在JSON中使用双引号(通过转义它们)。然后JSON.parse()将适当地接受输入字符串并正确解析。也删除方括号。

$scope.jsonString = "{\"items\":{\"itemname\":\"apple\",\"itemdesc\":\"fruit\"}}"; 

var test = JSON.parse($scope.jsonString); 
上侧

注意,您还可以使用JSON Lint来检查,如果你的JSON是有效的。

让我知道这是否有帮助。

+0

请检查编辑的问题 –

+0

您的父节点'项目'在行 - 'var jsondata =“{\项目\:”;'不包含双引号。请尝试'var jsondata =“{\”items \“:”;'。另请检查后续值以放置双引号。有逃避的'\'字符,但不是你将要逃跑的角色。 –

+0

请再次阅读该问题。 $ scope.product = {items:[{itemname:“apple”,itemdesc:“fruit”}]}; 正在运行代码 –