【问题标题】:How to use JSON to fill ObservableCollection?如何使用 JSON 填充 ObservableCollection?
【发布时间】:2019-06-29 16:14:24
【问题描述】:

如何使用 JSON 填充 ObservableCollection?现在桌面应用程序中只有脚本本身和模型。我不明白如何把它绑起来。 运行脚本后我得到它:

{
"records": [
    {
        "brand_id": "1",
        "brand_name": "Gigabyte"
    },
    {
        "brand_id": "2",
        "brand_name": "MSI"
    },
    {
        "brand_id": "3",
        "brand_name": "Lenovo"
    },
    {
        "brand_id": "4",
        "brand_name": "Dell"
    },
    {
        "brand_id": "5",
        "brand_name": "Google"
    }
]}

我在应用中有一个模型:

public class Brands
{
    int brand_id;
    string brand_name;

    public int Brand_id { get => brand_id; set => brand_id = value; }
    public string Brand_name { get => brand_name; set => brand_name = value; }
}

和收藏:

public class BrandsCollection
{
    private ObservableCollection<Brands> brands;

    public ObservableCollection<Brands> Brands { get => brands; set => brands = value; }
}

【问题讨论】:

    标签: c# php json observablecollection wpfdatagrid


    【解决方案1】:

    这相当简单,尤其是可以使用可简化大量工作的软件包。 Nuget 包System.Net.Http 将包含您从网络创建HttpClientGet() 您的JSON 所需的包。我推荐 Newtonsoft.Json 将 JSON 解析为 C# 对象。然后拥有该对象,您只需将DataGrid.ItemSource 设置为任何类型的对象数组即可生成列。

    一个简单的例子:

    首先,您将定义 JSON 数据的简单对象表示。 例如,如果您有以下数据:

    [
      {
        "Name":"Test",
        "Data": ["Item1","Item2"]
      },
      {
        "Name":"Test 2",
        "Data": ["Item3","Item4"]
      }
    ]
    

    您必须创建一个等效的 C# 表示。 基本上这是一个对象列表,所以:

    public class OuterObject : List<InnerObject> {}
    

    内部对象如下:

    public class InnerObject {
      public string Name { get; set; }
      public List<string> Data { get; set; }
    }
    

    定义了对象后,您可以执行以下操作:

    HttpClient client = new HttpClient { BaseAddress = new Uri("ADDRESS") };
    var json = await client.GetAsync("/ENDPOINT");
    JsonSerializer serializer = JsonSerializer.CreateDefault();
    
    using (StringReader reader = new StringReader(json))
    {
         using (JsonTextReader jsonReader = new JsonTextReader(reader))
         {
              var result = serializer.Deserialize<OuterObject>(jsonReader);
         }
    }
    

    然后要访问程序中的数据,您可以像这样访问它:

    string name = result[0].Name;
    

    或将其设置为 DataGrid's ItemSource 以使数据神奇地显示出来。

    grid1.ItemSource = result;
    

    只要结果是一个项目数组,它就会为每个项目创建一行。 您可能想要指定显示哪些项目,但这是通过修改 DataGrid.Columns 定义和设置 DataGrid.AutogenerateColumns = false 来完成的

    编辑:使用您的数据和模型

    //Just a small change to the Collection
    public class BrandsCollection {
        private ObservableCollection<Brands> _records;
    
        public ObservableCollection<Brands> records { get => _records; set => _records= value; }
    }
    

    然后解析数据...

    JsonSerializer serializer = JsonSerializer.CreateDefault();
    
    using (StringReader reader = new StringReader(json))
    {
         using (JsonTextReader jsonReader = new JsonTextReader(reader))
         {
              var result = serializer.Deserialize<BrandsCollection>(jsonReader);
         }
    }
    

    你必须记住要么使用与 json 标签相同的名称,要么使用 Json 属性。有关更多信息,您可以访问官方 Newtonsoft.Json documentation

    【讨论】:

    • 需要我创建一个新方法来解析对象或集合中的数据吗?
    • 什么是“/ENDPOINT”?
    • 你对数据的处理取决于你,但你有对象,所以你可以更轻松地处理它...... 至于端点,它是你获取数据的 URL。
    猜你喜欢
    • 2013-06-11
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    相关资源
    最近更新 更多