简体   繁体   English

ng repeat json parse显示空字符串

[英]ng repeat json parse show empty string

I had the array of objects string from a list of array. 我有数组列表中的对象字符串数组。 I convert object string to object using method of JSON.parse the object but it shows empty within ng-repeat. 我使用JSON.parse方法将对象字符串转换为对象。 JSON.parse对象,但在ng-repeat中显示为空。

Jade

.item.col-md-4(ng-repeat='p in searchData | filter: paginate | orderBy: sortKey ')
   // `p` is a object of string, for example "{id:2, name:'abel'}";
   - var property = JSON.parse("{{ p }}"); // Error at this line
   +AddPropertyCard(property)

SearchData 搜索数据

[{id:0, name:"abel"},{id:1, name:"julia"}]

Error 错误

var property = JSON.parse(""); var属性= JSON.parse(“”);

Unexpected token { in JSON at position 1 意外令牌{在JSON中的位置1

Update 1 : 更新1

Variable 'p' Seems to be object. 变量“ p”似乎是对象。

You have to access it using dot notations. 您必须使用点符号来访问它。 Like {{p.id}} or {{p.name}} . {{p.id}}{{p.name}}一样 This will show respective values. 这将显示相​​应的值。


JSON.parse expects object string in order to Parse it. JSON.parse需要对象字符串以便对其进行解析。

// Lets create a simple Object in javascript
var notStringObj = {
    "name": "John",
    "age": 30,
    "city": "New York"
};
console.log("JavaScript Object", notStringObj);

// Lets stringify (Convert in string) the object
var stringObj = JSON.stringify(notStringObj);
console.log("JavaScript Stringified Object", stringObj);

// Following is code to decode this object

// This will give you result you are expecting i.e. JavaScript Object
var obj1 = JSON.parse(stringObj);

// This will throw error - Unexpected token { in JSON at position 1
// Because this was plain object not a string
// JSON.parse expects object string in order to Parse it
var obj2 = JSON.parse(notStringObj)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM