Windows窗体C# JSON数据在格式化后不显示

u1ehiz5o  于 2023-10-21  发布在  Windows
关注(0)|答案(1)|浏览(132)

我试图从一个API中提取数据,并在DataGridView控件中显示数据,但在对数据进行格式化后,它显示:



Button1 Click事件的代码:

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中列出,但没有。

vh0rcniy

vh0rcniy1#

根据DataGridView.DataSource,您应该提供以下类型的值:

  • IList接口,包括一维数组。
  • IListSource接口,例如DataTable和DataSet类。
  • IBindingList接口,例如BindingList类。
  • IBindingListView接口,例如BindingSource类。

而您尝试解析的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.productsdataGridView1.DataSource

var postResponse = JsonConvert.DeserializeObject<PostResponse>(responseContent);
dataGridView1.DataSource = postResponse.Products;
dataGridView1.Refresh();

旁注:
1.因为你的方法是async,你应该使用await而不是/* Task */.Result来避免死锁。

var responseContent = response.Content.ReadAsStringAsync().Result

收件人:

var responseContent = await response.Content.ReadAsStringAsync();

1.使用Pascal大小写而不是camel大小写来描述属性名。

相关问题