场景

需要在Winform的页面初始化之后,调用http的接口,并将返回的json数据

解析,赋值到窗体中的TextBox组件中。

使用接口测试工具测试的接口返回数据如下

winform 调用javascript winform调用接口_数据



实现

1、调用get接口

var httpClient = new HttpClient();
            var url = new Uri("http://后台接口ip:8091/monitor/pathInfo/get?serverName=zlw");
            var response = httpClient.GetAsync(url).Result;
            var data = response.Content.ReadAsStringAsync().Result;

这里传递参数直接在url中拼接的方式传递,传递了一个参数为serverName,值为zlw

其中HttpClient需要引用

using System.Net.Http;

此时data的返回数据是json字符串,示例结果

{"msg":"操作成功","code":200,"data":{"id":7,"serverName":"zlw","mineCode":"","targetServer":"","port":"","user":"","pass":"","dir":"","filePath":"","uploadTime":"","uploadStatus":"","remark":"","creatDate":"2022-04-13 00:00:00","updateDate":"","extend":"","extend_2":"","extend_3":""}}

2、调用post接口并传参

var url = new Uri("http://后台接口Ip:8091/monitor/pathInfo/update");
                var body = new FormUrlEncodedContent(new Dictionary<string, string>
                {
                    {"serverName", "zlw"},
                    {"mineCode", this.textBox_mineCode.Text.Trim().ToString()},
                    {"targetServer",this.textBox_targetServer.Text.Trim().ToString()},
                    {"port", this.textBox_port.Text.Trim().ToString()},
                    {"user", this.textBox_user.Text.Trim().ToString()},
                    {"pass", this.textBox_pass.Text.Trim().ToString()},
                    {"dir", this.textBox_dir.Text.Trim().ToString()},
                    {"filePath", this.textBox_filePath.Text.Trim().ToString()},
                });
                // response
                var response = httpClient.PostAsync(url, body).Result;
                var data = response.Content.ReadAsStringAsync().Result;

传递参数通过这种键值对的方式进行赋值。

3、怎样将返回的json字符串数据进行解析为实例类。

首先获取到示例的返回json数据,然后通过JSON转换C#实体类的一些在线网站

https://www.nhooo.com/tool/json2cs/

https://www.json.cn/json/json2csharp.html

进行转换成实体类

winform 调用javascript winform调用接口_Text_02

 

 

比如上面转换的实体类就是

namespace bdtdDataUpload
{
    public class Data
    {
        /// <summary>
        ///
        /// </summary>
        public int id { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string serverName { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string mineCode { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string targetServer { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string port { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string user { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string pass { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string dir { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string filePath { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string uploadTime { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string uploadStatus { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string remark { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string creatDate { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string updateDate { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string extend { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string extend_2 { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string extend_3 { get; set; }

    }

 

    public class Root
    {
        /// <summary>
        /// 操作成功
        /// </summary>
        public string msg { get; set; }

        /// <summary>
        ///
        /// </summary>
        public int code { get; set; }

        /// <summary>
        ///
        /// </summary>
        public Data data { get; set; }

    }


}

转换之后注意类名与文件名一致。

然后在获取到结果时解析为实体类

var data = response.Content.ReadAsStringAsync().Result;
            JavaScriptSerializer js = new JavaScriptSerializer();//实例化一个能够序列化数据的类
            Root result = js.Deserialize<Root>(data);

这里的Root就是上面生成的实体类

这里需要引用

using System.Web.Script.Serialization;

然后就可以获取到对应的数据进行使用了

var response = httpClient.GetAsync(url).Result;
            var data = response.Content.ReadAsStringAsync().Result;
            //Console.WriteLine(data);
            JavaScriptSerializer js = new JavaScriptSerializer();//实例化一个能够序列化数据的类
            Root result = js.Deserialize<Root>(data);
            this.textBox_mineCode.Text = result.data.mineCode;
            this.textBox_targetServer.Text = result.data.targetServer;
            this.textBox_port.Text = result.data.port;
            this.textBox_user.Text = result.data.user;
            this.textBox_pass.Text = result.data.pass;
            this.textBox_dir.Text = result.data.dir;
            this.textBox_filePath.Text = result.data.filePath;