这是我正在尝试从以下位置读取数据的JSON文档:
{title": "some name here",
"details": {
"color": "red",
"location": "at home",
"shape": "square"
}}
我正在尝试在我的js文件中读取此数据,然后将其添加到html中。 这是我的代码:
$http.get('/api/dataIuse')
.success(function(rdata) {
$scope.title = rdata['title'];
$scope.details = rdata['details'];
$scope.fullData = rdata;
console.log(rdata['title']);
})
.error(function(rdata) {
console.log('Error: ' + rdata);
});
如何阅读“细节”内部以分别获取颜色,位置和形状?
这是我的猜测(它不起作用):
$scope.details.color = rdata['details.color'];
$scope.details.location = rdata['details.location'];
$scope.details.shape = rdata['details.shape'];
我应该使用什么方法来阅读JSON?
谢谢!
答案 0 :(得分:0)
尝试以下方法:
var rdata = {"title": "some name here",
"details": {
"color": "red",
"location": "at home",
"shape": "square"
}}
var color = rdata.details.color;
var area = rdata.details.location;
var shape = rdata.details.shape;
console.log(color);
console.log(area);
console.log(shape);
//or if you want to iterate through the object
Object.keys(rdata.details).forEach(function(item){
console.log(rdata.details[item]);
})