我想從JSON訪問元素並將其綁定到XAML。 我的JSON代碼:C#where select clause
{
"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();
}
}
沒有得到球員和goalminute的值。怎麼了?我試圖訪問一些值,他們工作正常,但是當我嘗試訪問事件數組它不work.help
發佈'結果'代碼,它是如何填充的? –
此外,你可以確認你的查詢返回一些結果嗎? (通過檢查調試模式) –
是的,GoalList.Itemsource查詢適用於其他某些where子句條件,但可能與此問題有一些樹問題。 – Nachiket