• winform调用WebApi


    金山词霸的api :https://dict-mobile.iciba.com/interface/index.php?c=word&m=getsuggest&nums=10&is_need_mean=1&word=hello

    首先根据Json的格式转成C#实体类 (在线工具:JSON转C#实体类-BeJSON.com

    {
        "message": [
            {
                "key": "hello",
                "paraphrase": "n.“喂”的招呼声或问候声;int.哈喽,喂,你好,您好,表示问候,打招呼",
                "value": 0,
                "means": [
                    {
                        "part": "int.",
                        "means": [
                            "哈喽,喂",
                            "你好,您好",
                            "表示问候",
                            "打招呼"
                        ]
                    },
                    {
                        "part": "n.",
                        "means": [
                            "“喂”的招呼声或问候声"
                        ]
                    }
                ]
            },
            {
                "key": "hellow",
                "paraphrase": "(通常的招呼语)嗨, (打电话用)喂!, (英)(表示惊讶)哎哟",
                "value": 475,
                "means": [
                    {
                        "part": "",
                        "means": [
                            "(通常的招呼语)嗨, (打电话用)喂!, (英)(表示惊讶)哎哟"
                        ]
                    }
                ]
            },
            {
                "key": "hello kitty",
                "paraphrase": "n.凯蒂猫(品牌名,另有译名堤提猫、凯迪猫、吉蒂猫等)",
                "value": 471,
                "means": [
                    {
                        "part": "n.",
                        "means": [
                            "凯蒂猫(品牌名,另有译名堤提猫、凯迪猫、吉蒂猫等)"
                        ]
                    }
                ]
            },
            {
                "key": "hello girl",
                "paraphrase": "女话务员,女电话接线员",
                "value": 389,
                "means": [
                    {
                        "part": "",
                        "means": [
                            "女话务员",
                            "女电话接线员"
                        ]
                    }
                ]
            },
            {
                "key": "hello everyone",
                "paraphrase": "大家好",
                "value": 384,
                "means": [
                    {
                        "part": "",
                        "means": [
                            "大家好"
                        ]
                    }
                ]
            },
            {
                "key": "hellos",
                "paraphrase": "",
                "value": 356
            },
            {
                "key": "hello goodbye",
                "paraphrase": "",
                "value": 174,
                "means": [
                    {
                        "part": "",
                        "means": [
                            "你好,再见(歌曲名)"
                        ]
                    }
                ]
            },
            {
                "key": "hello screen",
                "paraphrase": "问候屏",
                "value": 58,
                "means": [
                    {
                        "part": "",
                        "means": [
                            "问候屏"
                        ]
                    }
                ]
            }
        ],
        "status": 1
    }

    新建C#实体类

    public class MeansItem
    {
        public string part { get; set; }
        public List <string > means { get; set; }
    }
     
    public class MessageItem
    {
        public string key { get; set; }
        public string paraphrase { get; set; }
        public int value { get; set; }
        public List <MeansItem > means { get; set; }
    }
     
    public class Root
    {
        public List <MessageItem > message { get; set; }
        public int status { get; set; }
    }

    C#代码

            private void button1_Click(object sender, EventArgs e)
            {
                dataGridView1.Rows.Clear();
                string Url = "https://dict-mobile.iciba.com/interface/index.php?c=word&m=getsuggest&nums=10&is_need_mean=1&word={0}";
                Url = string.Format(Url, textBox1.Text);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "GET";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string json = reader.ReadToEnd();
                jinshan w = JsonConvert.DeserializeObject<jinshan>(json); //jinshan是主实体类的名称,就是上面的Root
                foreach (var m in w.message)
                {
                    dataGridView1.Rows.Add(m.key, m.paraphrase);
                }
            }

    实现效果

  • 相关阅读:
    HBase目录
    HBase介绍及简易安装(转)
    Hadoop目录
    通过java读取HDFS的数据 (转)
    Flume-NG一些注意事项(转)
    spark读取 kafka nginx网站日志消息 并写入HDFS中(转)
    Java开发牛人十大必备网站
    HTTP协议状态码详解(HTTP Status Code)(转)
    Flume NG 配置详解(转)
    Android笔记(五)利用Intent启动活动
  • 原文地址:https://www.cnblogs.com/fangjb/p/15786878.html
Copyright © 2020-2023  润新知