在gridview上用c#显示来自rest api服务器的请求

Posted

技术标签:

【中文标题】在gridview上用c#显示来自rest api服务器的请求【英文标题】:Show the request from rest api server with c # on gridview 【发布时间】:2021-05-23 09:21:16 【问题描述】:

我正在用 c# 连接到 rest api 服务器。一个json输出如下。

WindowsForms、Model和Repository文件如下。

1-) 换句话说,我想将json数据传入的“结果”对象传输到模型文件中,并在gridview上显示。

2-) 另外,当我发出“Get”请求时,我想请求一个带有参数的“Body”对象。 示例:"business_code": "dental"

CustomersModel.cs

 namespace HastaTakip.Models
 
     public class CustomersModel
     
         public result _result  get; set; 
    
         public class result
         
             public int id  get; set; 
             public string customer_name  get; set; 
             public string customer_lastname  get; set; 
             public string customer_identity  get; set; 
             public string customer_gender  get; set; 
             public string customer_phone  get; set; 
             public string customer_description  get; set; 
             public bool customer_status  get; set; 
             public string doctor  get; set; 
             public string customer_code  get; set; 
             public string business_code  get; set; 
             public int user_code_id  get; set; 
        
     
 

CustomersRepository.cs

using System.Net.Http;
using System;
using System.Threading.Tasks;
using HastaTakip.Models;
using Newtonsoft.Json;

namespace HastaTakip.Api

    public class CustomersRepository
    
        public HttpClient _client;
        public HttpResponseMessage _response;
    
        public CustomersRepository()
        
            _client = new HttpClient();
            _client.BaseAddress = new Uri("http://localhost:3000/");
            _client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
        

        public async Task<CustomersModel> GetList()
        
            _response = await _client.GetAsync($"customers");
            var json = await _response.Content.ReadAsStringAsync();
            var listCS = JsonConvert.DeserializeObject<CustomersModel>(json);
            return listCS;
        
    

WindowsForms.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HastaTakip.Api;
using HastaTakip.Models;

namespace HastaTakip.Forms

    public partial class frmCustomerList : Form
    
        CustomersRepository _repository = new CustomersRepository();

        public frmCustomerList()
        
            InitializeComponent();
            LoadData();
        

        private async void LoadData()
        
            var listCS = await _repository.GetList();
            gridControl1.DataSource = listCS;
        

        private  void frmCustomerList_Load(object sender, EventArgs e)
        
        
    

Json 结果


    "result": [
    
        "id": 1,
        "customer_name": "Test1",
        "customer_lastname": null,
        "customer_identity": "54sd45",
        "customer_gender": "Man",
        "customer_phone": null,
        "customer_description": "kkjkjk.",
        "customer_status": null,
        "doctor": null,
        "customer_code": "bcc50586-6960-4766-9468-c9dc55780e40",
        "business_code": "dental",
        "user_code_id": 1
    ,
    
        "id": 2,
        "customer_name": "Depron",
        "customer_lastname": null,
        "customer_identity": "564434",
        "customer_gender": "WOMEN",
        "customer_phone": null,
        "customer_description": "record test.",
        "customer_status": null,
        "doctor": null,
        "customer_code": "344865b4-1028-4051-9ec4-71db17414787",
        "business_code": "dental",
        "user_code_id": 1
    
    ]

【问题讨论】:

【参考方案1】:

您应该从 json 文件中返回一个列表或数组,但在您的模型中,您只有一个结果,正确的方法是:

public class CustomersModel
     
         public List<result> _result  get; set;            
     
  public class result
         
             public int id  get; set; 
             public string customer_name  get; set; 
             public string customer_lastname  get; set; 
             public string customer_identity  get; set; 
             public string customer_gender  get; set; 
             public string customer_phone  get; set; 
             public string customer_description  get; set; 
             public bool customer_status  get; set; 
             public string doctor  get; set; 
             public string customer_code  get; set; 
             public string business_code  get; set; 
             public int user_code_id  get; set; 
        

除非有很好的理由,否则我永远不会使用嵌套类。只要它不是私人的,我无论如何都看不到这里的原因。

【讨论】:

非常感谢您的回答。但正如你所说,我对代码进行了更改,但它返回“null”。错误图片:ibb.co/3f3f8VP 其实我注意到了。我正在获取数据,但它没有将其导出到模型中的“结果”中。

以上是关于在gridview上用c#显示来自rest api服务器的请求的主要内容,如果未能解决你的问题,请参考以下文章

无法显示来自 Wordpress REST API 自定义端点的自定义字段

使用 C# REST API 从 TFS 获取用户身份

创建一个 .NET Core Rest API 应用程序以支持来自应用程序的不同数据库

C# 中dev gridview怎么在最左边显示序号,如图显示

从 ftp 在 gridview 上显示数据 查看更多:C#ASP.NET

调试器在解析 Rest API 响应 HttpClient、Xamarin Forms 时停止