我有以下json:
{
"COMPETITIONS": [
{
"name": "Name 1",
"id": "76-7011",
"ltable": "0"
},
{
"name": "Name 2",
"id": "116-7",
"ltable": "0"
},
{
"name": "Name 3",
"id": "217-68",
"ltable": "0"
}
]
}
使用Json.net
将其反序列化为动态 dynamic jsonResponse = JsonConvert.DeserializeObject(result);
我想将此数据传递到列表框,例如
lb.ItemsSource = jsonResponse
<Page.Resources>
<DataTemplate x:Key="DataTemplate1">
<Grid>
<TextBlock x:Name="textBox" TextWrapping="Wrap" Text="{Binding name}" d:LayoutOverrides="Width, Height" Foreground="Black"/>
</Grid>
</DataTemplate>
</Page.Resources>
但它告诉我:
错误:BindingExpression路径错误:&#39; name&#39;找不到物业 &#39; Newtonsoft.Json.Linq.JObject,Newtonsoft.Json,Version = 7.0.0.0, Culture = neutral,PublicKeyToken = 30ad4fe6b2a6aeed&#39;。 BindingExpression: 路径=&#39;名称&#39; DataItem =&#39; Newtonsoft.Json.Linq.JObject,Newtonsoft.Json, Version = 7.0.0.0,Culture = neutral,PublicKeyToken = 30ad4fe6b2a6aeed&#39 ;; 目标元素是&#39; Windows.UI.Xaml.Controls.TextBlock&#39; (名称=&#39;的textBox&#39);目标属性是&#39; Text&#39; (键入&#39; String&#39;)
任何建议请将其解析为列表框而不要偏离动态和使用类?
答案 0 :(得分:1)
您可以创建类以反序列化您的JSON。
public class COMPETITION
{
public string name { get; set; }
public string id { get; set; }
public string ltable { get; set; }
}
public class RootObject
{
public List<COMPETITION> COMPETITIONS { get; set; }
}
比你可以使用下面的代码反序列化
var jsonResponse = JsonConvert.DeserializeObject<COMPETITION>(result);