提问人:gb00- 提问时间:10/6/2023 最后编辑:Brian Tompsett - 汤莱恩gb00- 更新时间:10/7/2023 访问量:57
Windows 窗体 C# JSON 数据在反序列化后不显示
Windows Forms C# JSON Data doesn't show after deserialization
问:
我正在尝试从 API 中提取数据并显示控件中的数据,但在反序列化数据后,它显示:DataGridView
.
Click 事件的代码:Button1
private async void button1_Click(object sender, EventArgs e)
{
try
{
var postData = new PostData
{
id=5,
title = "yeni",
description = "yenidesc",
type_name="yenitype"
};
var client = new HttpClient();
client.BaseAddress = new Uri("-i_know_that_my_uri_should_be_here-");
var json = JsonConvert.SerializeObject(postData);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = client.PostAsync("products",content).Result;
if (response.IsSuccessStatusCode)
{
var responseContent = response.Content.ReadAsStringAsync().Result;
var postResponse = JsonConvert.DeserializeObject<PostResponse>(responseContent); //JsonSerializer.Deserialize<PostResponse>(responseContent);
MessageBox.Show("help: "+ postResponse);
dataGridView1.DataSource = postResponse;
dataGridView1.Refresh();
}
else
{
MessageBox.Show("Hata: " + response.StatusCode);
}
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
这是我的班级:PostData
public class PostData
{
public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
public string type_name { get; set; }
}
这是我的班级:PostResponse
public class PostResponse
{
public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
public string type_name { get; set; }
}
这是我的JSON文件:
{"message":"product data has been taken successfully","products":[{"id":116,"title":"GASA","description":"fdsafsd","situation":1,"type_name":"Kasa","type_id":1,"features":[{"products_type_feature_id":56,"feature_name":"Marka","feature_unit_name":"","feature":"efwef"},{"products_type_feature_id":57,"feature_name":"Model","feature_unit_name":"","feature":"wefw"},{"products_type_feature_id":58,"feature_name":"Kasa Tipi","feature_unit_name":"","feature":""},{"products_type_feature_id":59,"feature_name":"Kasa Boyutu","feature_unit_name":"","feature":"fwef"},{"products_type_feature_id":60,"feature_name":"Renk","feature_unit_name":"","feature":"fewe"},{"products_type_feature_id":61,"feature_name":"Malzeme","feature_unit_name":"","feature":""},{"products_type_feature_id":62,"feature_name":"Soğutma Sistemi","feature_unit_name":"","feature":"wefw"},{"products_type_feature_id":63,"feature_name":"Ön Panel Bağlantı Noktaları","feature_unit_name":"","feature":"efwef"},{"products_type_feature_id":64,"feature_name":"Ağırlık","feature_unit_name":"kg","feature":"fwefwef"},{"products_type_feature_id":65,"feature_name":"İşlemci Modeli","feature_unit_name":"","feature":""},{"products_type_feature_id":66,"feature_name":"Anakart Modeli","feature_unit_name":"","feature":""},{"products_type_feature_id":67,"feature_name":"Bellek Kapasitesi","feature_unit_name":"GB","feature":""},{"products_type_feature_id":68,"feature_name":"HDD Kapasitesi","feature_unit_name":"GB","feature":""},{"products_type_feature_id":69,"feature_name":"SSD Kapasitesi","feature_unit_name":"GB","feature":""},{"products_type_feature_id":70,"feature_name":"Ekran Kartı Modeli","feature_unit_name":"","feature":""},{"products_type_feature_id":71,"feature_name":"Ekran Kartı Kapasitesi","feature_unit_name":"GB","feature":""},{"products_type_feature_id":72,"feature_name":"İşletim Sistemi","feature_unit_name":"","feature":""},{"products_type_feature_id":73,"feature_name":"Güç Kaynağı Kapasitesi","feature_unit_name":"Watt","feature":""}]},{"id":115,"title":"nııgaaa","description":"gfdgfd","situation":1,"type_name":"Laptop","type_id":2,"features":[{"products_type_feature_id":43,"feature_name":"Marka","feature_unit_name":"","feature":"a"},{"products_type_feature_id":44,"feature_name":"Model","feature_unit_name":"","feature":"a"},{"products_type_feature_id":45,"feature_name":"Ekran Boyutu","feature_unit_name":"inç","feature":"a"},{"products_type_feature_id":46,"feature_name":"İşlemci Modeli","feature_unit_name":"","feature":"a"},{"products_type_feature_id":47,"feature_name":"Bellek Kapasitesi","feature_unit_name":"GB","feature":"a"},{"products_type_feature_id":48,"feature_name":"HDD Kapasitesi","feature_unit_name":"GB","feature":"a"},{"products_type_feature_id":49,"feature_name":"SSD Kapasitesi","feature_unit_name":"GB","feature":"a"},{"products_type_feature_id":50,"feature_name":"Ekran Kartı Modeli","feature_unit_name":"","feature":"a"},{"products_type_feature_id":51,"feature_name":"Ekran Kartı Kapasitesi","feature_unit_name":"GB","feature":""},{"products_type_feature_id":52,"feature_name":"İşletim Sistemi","feature_unit_name":"","feature":""},{"products_type_feature_id":53,"feature_name":"Batarya Ömrü","feature_unit_name":"saat","feature":""},{"products_type_feature_id":54,"feature_name":"Ağırlık","feature_unit_name":"kg","feature":""}]}]}
顺便说一句,这是我在 StackOverflow 上的第一篇文章。如果我犯了任何错误,我深表歉意。
我试过使用 ,但结果仍然相同。
我本来以为它会正常工作并列出,但不,它没有。System.Text.Json
DataGridView
答:
0赞
Yong Shun
10/6/2023
#1
根据 DataGridView.DataSource
,应提供以下类型的值:
接口,包括一维数组。
IList
接口,例如 DataTable 和 DataSet 类。
IListSource
接口,如 BindingList 类。
IBindingList
接口,如 BindingSource 类。
IBindingListView
而您尝试反序列化的 JSON 响应与结构不匹配。结构应为:PostResponse
PostResponse
using System.Collections.Generic;
public class PostResponse
{
public string Message { get; set; }
public List<ProductData> Products { get; set; }
}
public class ProductData
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
[JsonProperty("type_name")]
public string TypeName { get; set; }
}
您应该提供 给 .postResponse.products
dataGridView1.DataSource
var postResponse = JsonConvert.DeserializeObject<PostResponse>(responseContent);
dataGridView1.DataSource = postResponse.Products;
dataGridView1.Refresh();
旁注:
- 由于您的方法是 ,因此您应该使用 代替 来避免死锁。
async
await
/* Task */.Result
var responseContent = response.Content.ReadAsStringAsync().Result
自:
var responseContent = await response.Content.ReadAsStringAsync();
- 使用 Pascal 大小写而不是驼峰大小写声明属性名称。
评论
0赞
gb00-
10/6/2023
非常感谢。它现在正在工作,我能够在 datagridview 中列出 api 数据。非常感谢。
评论
"help: "+ postResponse
.ToString()
PostResponse