我的 JSON 看起来像这样:
[
{
"id": 001,
"name": "Item 1",
"tree": [
"010",
"020",
"030"
]
},
{
"id": 002,
"name": "Item 2",
"tree": [
"010",
"020",
"030"
]
},
{
"id": 003,
"name": "Item 3",
"tree": [
"010",
"020",
"030"
]
}
]
这可以建模为 C#,如下所示:
public class Product
{
public int id { get; set; }
public string name { get; set; }
public List<string> tree { get; set; }
}
我正在尝试将此 JSON 数据显示到 ObjectListView 库中的 TreeListView 中。理想情况下,它看起来像这样。
我当前的代码如下,“数据”是 TreeListView。
List<Product> Products = JsonConvert.DeserializeObject<List<Product>>(json);
data.CanExpandGetter = model => ((Product)model).tree.Count > 0;
data.ChildrenGetter = delegate(object model)
{
return ((Product)model).
tree;
};
data.SetObjects(Products);
但是,这会引发System.InvalidCastException
at model => ((Product)model).tree.Count > 0
。