【问题标题】:how to get data from multyline json object using Angular如何使用 Angular 从多行 json 对象中获取数据
【发布时间】:2014-11-20 10:05:38
【问题描述】:

在 $http 获取请求资源 api 后返回多行 json:(示例)

{"features":["type": "feature", "properties":{ "id": "001", "name": "Example"}]}

如何从该数据中获取 id?

$scope.myDate = $http.get('http://example.com/').
  success(function(data, status){
    var test = data.features;
    $scope.information = test['type'];

  }).
  error(function(data, status){
    console.log('not');
  });

我使用的方式不正确,有什么建议吗? 提前谢谢你!

【问题讨论】:

  • 这个 json 似乎无效, features 是一个数组,并且包含键值。应该是一个对象

标签: json angularjs http


【解决方案1】:

你的JSON无效,需要按如下修改后再试

var data = {
    "features": [{
        "type": "feature",
        "properties": {
            "id": "001",
            "name": "Example"
        }
    }]
};

alert(data.features[0].type);

注意:features 数组包含对象,所以需要用{} 括起来

【讨论】:

  • 如果你想测试,使用jslint.com并粘贴到你的json中;它会为你验证。
  • thanx,如果 feature 中有多个对象,我应该先对其进行迭代以将结果放入数组中吗?
  • 是的。 {"features":[{"id":"001"},{"id":"002"}]}
猜你喜欢
  • 2020-05-05
  • 1970-01-01
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
  • 2017-02-13
  • 1970-01-01
相关资源
最近更新 更多