我正在尝试从JSON对象获取一些特定数据。
我能够从括号外获取数据,但无法从中获取任何内容。
我想得到的是从下面获取"summonerName"
。
"gameId": 2016761444,
"mapId": 11,
"gameMode": "CLASSIC",
"gameType": "MATCHED_GAME",
"gameQueueConfigId": 4,
"participants": [
{
"teamId": 100,
"spell1Id": 4,
"spell2Id": 12,
"championId": 57,
"profileIconId": 615,
"summonerName": "Wolves Weekend",
"bot": false,
"summonerId": 22951400,
]
},
还有9个“参与者”对象。
这就是我目前正在尝试的方式:
div(ng-repeat='data in info.display')
p(ng-model='info.IdBox') {{data.id}}
p Level: {{data.summonerLevel}}
p Champion: {{data.championId}}
{{info.matchInfo}}
ul
div(ng-repeat='participant in info.matchInfo')
p Name: {{matchInfo.participants[participant].summonerName}}
如前所述,简单的通话工作正常......
有什么想法吗?
谢谢〜
答案 0 :(得分:1)
首先,您的JSON在participants
对象中无效。
您现在的JSON示例:
"participants": [
{
"teamId": 100,
"spell1Id": 4,
"spell2Id": 12,
"championId": 57,
"profileIconId": 615,
"summonerName": "Wolves Weekend",
"bot": false,
"summonerId": 22951400, //Extra semicolon and missing curly brace.
]
}
}
应该是:
{
"gameId": 2016761444,
"mapId": 11,
"gameMode": "CLASSIC",
"gameType": "MATCHED_GAME",
"gameQueueConfigId": 4,
"participants": [
{
"teamId": 100,
"spell1Id": 4,
"spell2Id": 12,
"championId": 57,
"profileIconId": 615,
"summonerName": "Wolves Weekend",
"bot": false,
"summonerId": 22951400
}
]
}
请注意,您希望在包含数组的对象中检索内容。您需要识别要读取的数组中的项目,然后指定要读取的键。
您需要做的就是访问数组中的子对象。您可以通过提供要访问的数组中项目的index
来执行此操作。
var jsonObject = 'yourJsonDataWouldBeHere...';
var mySelectedField = jsonObject.participants[1].summonerId;
当前代码运行示例:
var app = angular.module("MyApp", []);
app.controller("MyCtrl", function($scope){
$scope.jsonData = {
"gameId": 2016761444,
"mapId": 11,
"gameMode": "CLASSIC",
"gameType": "MATCHED_GAME",
"gameQueueConfigId": 4,
"participants": [
{
"teamId": 100,
"spell1Id": 4,
"spell2Id": 12,
"championId": 57,
"profileIconId": 615,
"summonerName": "Wolves Weekend",
"bot": false,
"summonerId": 22951400
},
{
"teamId": 101,
"spell1Id": 5,
"spell2Id": 13,
"championId": 58,
"profileIconId": 616,
"summonerName": "Bear Something",
"bot": false,
"summonerId": 22951400
}
]
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp">
<div ng-controller="MyCtrl">
<!--I'm accessing the ARRAY directly...-->
<ul ng-repeat="item in jsonData.participants">
<li>{{item.summonerName}}</li>
</ul>
</div>
</div>