使用ionic或AngularJS的JSON@attributes
我很难理解如何读取包含“@attributes”的JSON对象 控制器:使用ionic或AngularJS的JSON@attributes,json,angularjs,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"];
$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提要
{
@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",
}
}
但我有个问题它不能提供数据。。请帮帮我 看起来数据是以文本形式读取的,因此您必须对其进行解析以使其正常工作
$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;
为了便于阅读,我建议您将匹配项中的所有@attribute映射到一个新数组中:
var matchFiltered = matches.map(function(item){ return item['@attributes']; });
//Getting the properties is easier now
var matchName1 = matchFiltered[0].match_name;
你似乎正在进行跨域呼叫。浏览器不允许这样。了解更多@Bro,我已经添加了访问控制允许来源。并且在chrome控制台中没有错误。还有console.log(响应);查看提要。