JSON @attributes使用ionic或AngularJS

时间:2015-07-07 06:05:20

标签: json angularjs

我很难理解如何阅读包含" @ attributes"的JSON对象。

控制器:

    $http.get('http://nskfix.com/dev/BettingApp/Feeds/PLFeeds.php').then(function(response){
          console.log(response);
          getdata=response.data.Competitions.Competition.Matches.Match["@attributes"];

JSON Feed

    {
@attributes: {
time: "1436244726"
},
Competitions: {
Competition: {
@attributes: {
name: "Premier League",
id: "ENG_1",
season: "2015/2016",
country: "England",
country_id: "ENG"
},
Matches: {
Match: [
{
@attributes: {
match_id: "03c44f8c67d215c39ea29ca821444c55",
match_name: "Southampton-Crystal Palace",
match_date: "2016-05-15 16:00:00"
}
},
{
@attributes: {
match_id: "5a46e12304adc60ee0ee365c2a83841e",
match_name: "Stoke-West Ham",
match_date: "2016-05-15 16:00:00",
match_status: "notstarted",
}
}

但是我有问题它没有提供数据..请帮助我!

1 个答案:

答案 0 :(得分:0)

似乎数据被读作文本,因此您必须对其进行解析才能使其正常工作。

$http.get('http://nskfix.com/dev/BettingApp/Feeds/PLFeeds.php').then(function(response){
      var data = JSON.parse(response.data);
      var matches = data.Competitions.Competition.Matches.Match; 
      //An array of object with @attributes field
}

然后你可以从数组中获取匹配信息:

var matchName1 = matches[0]['@attributes'].match_name;
var matchName2 = matches[1]['@attributes'].match_name;

为了便于阅读,我建议您将matches数组中的所有@attribute映射到一个新数组中:

var matchFiltered = matches.map(function(item){ return item['@attributes'];  });
//Getting the properties is easier now
var matchName1 = matchFiltered[0].match_name;