2017-07-06 53 views
-2

我想一个JSON对象发送到我的apiController:JSON阵列,quotaions标记

myJson对象,我构建dynamicaly这样的:

var course= new Object(); 
course.info= $scope.info; 
course.studentList = []; 
course.studentList = $scope.results; 

$ scope.results是一个数组我从我的观点得到:

<select ng-model="results[$index]" > 
    <option value=""></option> 
    <option ng-repeat="r in myList" value={{r}}>{{r.studlastname}}</option> 
</select> 

我的期望:

{ 
"course": { 
    "courseName":"yyy", 
    "courseDesc":"xxxx", 
    "prof":"prof1" 
}, 
"studentList" :[ 
    {"studfirstname":"2","studlastname":"","studAge":"21"}, 
    {"studfirstname":"4","studlastname":"","studAge":"40"}, 
    {"studfirstname":"6","studlastname":"","studAge":"60"} 
] 
} 

我有:

{ 
"course": { 
    "courseName":"yyy", 
    "courseDesc":"xxxx", 
    "prof":"prof1" 
}, 
"studentList" :[ 
    "{"studfirstname":"2","studlastname":"","studAge":"21"}", 
    "{"studfirstname":"4","studlastname":"","studAge":"40"}", 
    "{"studfirstname":"6","studlastname":"","studAge":"60"}" 
] 
} 

通知每个studentList元素上的报价,这是在服务器端反序列化造成的问题。

请你能帮我删除报价吗?

谢谢你提前。

回答

0

从我所看到的,这里的问题是这样

course.studentList = $scope.results; 

因为该行字符串添加到您的JSON对象为NG-模型给出的,而不是对象,你的字符串,你在JSON阵列所添加的。

course.studentList = JSON.parse($scope.results); 
+0

嗨Wasif, 我已经尝试过这一点,但它给我这个错误: **语法错误:意外的标记,在JSON位置112处** – anonymouslyGeek

+0

是的,它是给错误作为你的JSON是无效的。你能告诉我这是ng-model的价值吗? {“studfirstname”:“2”,“studlastname”:“”,“studAge”:“21”}对不对? 这是无效的JSON,如果你尝试将它添加到变量来解析它。它应该像引用转义,在JSON对象中解析 示例: var v =“{\”studfirstname \“:\”2 \“,\”studlastname \“:\”\“,\”studAge \“: \ “21 \”}“; 然后你可以做这样的事情: JSON.parse(v); –

+0

正是我想要做的,但我无法弄清楚如何逃避这些引号! – anonymouslyGeek

0

我发现了一个简单的解决方案:

let objectArray = $scope.results.map((each)=>{return JSON.parse(each)}); 
course.studentsList = objectArray; 

这个解决我的问题。