我正在尝试从JSON访问元素并将其绑定到XAML。 我的JSON代码:
{
"id":1474976,
"date":"2014-02-12 20:00:00",
"dateiso":"2014-02-12T20:00:00+00:00",
"competition_id":831975,
"competition":"England Premier League",
"group":"",
"home_id":9879,
"home":"Fulham",
"homeshort":"Fulham",
"homepath":"fulham",
"away_id":8650,
"away":"Liverpool",
"awayshort":"Liverpool",
"awaypath":"liverpool",
"status":"Finished",
"halftime":[
1,
1
],
"fulltime":[
2,
3
],
"extratime":[
0,
0
],
"penalties":[
0,
0
],
"incidents":[
{
"id":2762178,
"type":"Own Goal",
"goaltype":"Own goal",
"team_id":9879,
"team":"Fulham",
"teamshort":"Fulham",
"teampath":"fulham",
"player_id":30831,
"player":"Kolo Tour\u00e9",
"playershort":"K. Tour\u00e9",
"minute":8
},
{
"id":2762179,
"type":"Yellow",
"goaltype":null,
"team_id":9879,
"team":"Fulham",
"teamshort":"Fulham",
"teampath":"fulham",
"player_id":26782,
"player":"William Kvist",
"playershort":"W. Kvist",
"minute":9
},
{
"id":2762207,
"type":"Goal",
"goaltype":"Regular goal",
"team_id":8650,
"team":"Liverpool",
"teamshort":"Liverpool",
"teampath":"liverpool",
"player_id":51553,
"player":"Daniel Sturridge",
"playershort":"D. Sturridge",
"minute":41
},
{
"id":2762238,
"type":"Yellow",
"goaltype":null,
"team_id":9879,
"team":"Fulham",
"teamshort":"Fulham",
"teampath":"fulham",
"player_id":31290,
"player":"Sascha Riether",
"playershort":"S. Riether",
"minute":50
},
{
"id":2762248,
"type":"Yellow",
"goaltype":null,
"team_id":8650,
"team":"Liverpool",
"teamshort":"Liverpool",
"teampath":"liverpool",
"player_id":184536,
"player":"Philippe Coutinho",
"playershort":"P. Coutinho",
"minute":56
},
{
"id":2762260,
"type":"Goal",
"goaltype":"Regular goal",
"team_id":9879,
"team":"Fulham",
"teamshort":"Fulham",
"teampath":"fulham",
"player_id":30352,
"player":"Kieran Richardson",
"playershort":"K. Richardson",
"minute":64
},
{
"id":2762262,
"type":"Yellow",
"goaltype":null,
"team_id":9879,
"team":"Fulham",
"teamshort":"Fulham",
"teampath":"fulham",
"player_id":30352,
"player":"Kieran Richardson",
"playershort":"K. Richardson",
"minute":65
},
{
"id":2762279,
"type":"Goal",
"goaltype":"Regular goal",
"team_id":8650,
"team":"Liverpool",
"teamshort":"Liverpool",
"teampath":"liverpool",
"player_id":184536,
"player":"Philippe Coutinho",
"playershort":"P. Coutinho",
"minute":72
},
{
"id":2762296,
"type":"Yellow",
"goaltype":null,
"team_id":8650,
"team":"Liverpool",
"teamshort":"Liverpool",
"teampath":"liverpool",
"player_id":156008,
"player":"Jordan Henderson",
"playershort":"J. Henderson",
"minute":77
},
{
"id":2762307,
"type":"Goal",
"goaltype":"Penalty",
"team_id":8650,
"team":"Liverpool",
"teamshort":"Liverpool",
"teampath":"liverpool",
"player_id":30618,
"player":"Steven Gerrard",
"playershort":"S. Gerrard",
"minute":90
},
{
"id":2762311,
"type":"Yellow",
"goaltype":null,
"team_id":9879,
"team":"Fulham",
"teamshort":"Fulham",
"teampath":"fulham",
"player_id":30839,
"player":"Johnny Heitinga",
"playershort":"J. Heitinga",
"minute":90
},
{
"id":2762312,
"type":"Yellow",
"goaltype":null,
"team_id":8650,
"team":"Liverpool",
"teamshort":"Liverpool",
"teampath":"liverpool",
"player_id":30618,
"player":"Steven Gerrard",
"playershort":"S. Gerrard",
"minute":90
}
]
}
我的c#代码:
GoalsList.ItemsSource =
results.Where(x=>x.id==football.Livestring).Select(y=>y.incidents)
.Select(z=>z.Select(m=>new
{
player=m.player.ToString(),
goalminute=m.minute.ToString(),
})).ToList();
Xaml代码:
<ListBox Name="GoalsList">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding player}" Foreground="White">
<Run Text="{Binding goalminute}" Foreground="White"></Run>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
public void getId(object sender, DownloadStringCompletedEventArgs e)
{
try
{
if (!string.IsNullOrEmpty(e.Result))
{
GoalsList.ItemsSource = results.Where(x => x.id==football.Livestring)
.Select(y => y.incidents)
.Select(z => z.Select(m => new
{
player = m.player,
goalminute = m.minute,
})).ToList();
}
}
没有获得球员和球门分钟的价值。怎么了?我尝试访问一些值,它们工作正常,但当我尝试访问事件数组时,它不起作用。帮助
答案 0 :(得分:1)
我认为你需要一个平面列表,但你得到列表清单。尝试使用SelectMany
这样的方法:
GoalsList.ItemsSource = results.Where(x => x.id == football.Livestring)
.Select(y => y.incidents)
.SelectMany(z => z.Select(m => new
{
player = m.player,
goalminute = m.minute,
})).ToList();
答案 1 :(得分:0)
1)你可以调试(步入)你的代码吗?
2)如果您可以先将LINQ划分为多个语句,将会很有用,例如:
var football = results.Where(x => x.id==football.Livestring);
var incidents = football.Select(y => y.incidents);
var playerGoalMinute = incidents.Select(z => z.Select(m => new
{
player = m.player,
goalminute = m.minute,
});
GoalsList.ItemsSource = playerGoalMinute.ToList();
并检查每个变量(或其Count)是否符合预期。