一、前言
最近工作客戶需要一個(gè)HTTP的Mes需求,所以自己去學(xué)習(xí)了C#請求HTTP的方法以及JSON數(shù)據(jù)的解析方法,總結(jié)出了點(diǎn)經(jīng)驗(yàn),以便后續(xù)自己找起來方便一點(diǎn),故在此寫一篇文章。
二、準(zhǔn)備工作
下面我用一個(gè)聚合數(shù)據(jù)提供的天氣預(yù)報(bào)API接口來闡述請求HTTP和JSON數(shù)據(jù)解析的功能;
先看API文檔這么訪問數(shù)據(jù)的
?可以看到需要的東西有請求地址、請求參數(shù)、請求方式、內(nèi)容類型,其中請求參數(shù)要有查詢城市的代碼和Key,Key就是個(gè)人賬戶請求訪問數(shù)據(jù)的密鑰,這個(gè)接口是免費(fèi)的,所以有需求的直接申請就行啦。
下面是代碼實(shí)例,需要添加的命名空間
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
三、開始干活
?請求示例
//請求地址
string URl = "http://apis.juhe.cn/simpleWeather/query";
//請求參數(shù)
string Key = "630abb8f3ecec761c7e88738a2353d";//這個(gè)是我的密鑰(不完整)用自己的密鑰
string City = "%E5%B9%BF%E5%B7%9E"; //城市代碼-廣州的
//請求服務(wù)器
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URl); //請求地址
string strdata = "city=" + City + "&" + "key=" + Key; //請求參數(shù)
myRequest.Method = "POST"; //請求方式
myRequest.ContentType = "application/x-www-form-urlencoded"; //請求內(nèi)容類型
//發(fā)送
StreamWriter sw = new StreamWriter(myRequest.GetRequestStream());
sw.Write(strdata);
sw.Close();
接收數(shù)據(jù)
上面就是訪問數(shù)據(jù)的方法,下面是接收服務(wù)器返回來數(shù)據(jù)的方法
//響應(yīng)結(jié)果
string strWebData = string.Empty;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
strWebData = reader.ReadToEnd();//返回來的所以數(shù)據(jù)
Console.WriteLine(strWebData);
這個(gè)是打印出來返回來的數(shù)據(jù)?
{"reason":"查詢成功!","result":{"city":"廣州","realtime":{"temperature":"25","humidity":"29","info":"晴","wid":"00","direct":"東北風(fēng)","power":"2級","aqi":"60"},"future":[{"date":"2023-03-05","temperature":"11\/26℃","weather":"晴","wid":{"day":"00","night":"00"},"direct":"持續(xù)無風(fēng)向"},{"date":"2023-03-06","temperature":"11\/26℃","weather":"晴","wid":{"day":"00","night":"00"},"direct":"持續(xù)無風(fēng)向"},{"date":"2023-03-07","temperature":"14\/26℃","weather":"晴","wid":{"day":"00","night":"00"},"direct":"持續(xù)無風(fēng)向"},{"date":"2023-03-08","temperature":"15\/26℃","weather":"多云","wid":{"day":"01","night":"01"},"direct":"持續(xù)無風(fēng)向"},{"date":"2023-03-09","temperature":"16\/27℃","weather":"多云","wid":{"day":"01","night":"01"},"direct":"持續(xù)無風(fēng)向"}]},"error_code":0}
下面這個(gè)json格式數(shù)據(jù)的結(jié)構(gòu)看起來更直觀一點(diǎn)?
{
"reason": "查詢成功!",
"result": {
"city": "廣州",
"realtime": {
"temperature": "25",
"humidity": "29",
"info": "晴",
"wid": "00",
"direct": "東北風(fēng)",
"power": "2級",
"aqi": "60"
},
"future": [
{
"date": "2023-03-05",
"temperature": "11/26℃",
"weather": "晴",
"wid": {
"day": "00",
"night": "00"
},
"direct": "持續(xù)無風(fēng)向"
},
{
"date": "2023-03-06",
"temperature": "11/26℃",
"weather": "晴",
"wid": {
"day": "00",
"night": "00"
},
"direct": "持續(xù)無風(fēng)向"
},
{
"date": "2023-03-07",
"temperature": "14/26℃",
"weather": "晴",
"wid": {
"day": "00",
"night": "00"
},
"direct": "持續(xù)無風(fēng)向"
},
{
"date": "2023-03-08",
"temperature": "15/26℃",
"weather": "多云",
"wid": {
"day": "01",
"night": "01"
},
"direct": "持續(xù)無風(fēng)向"
},
{
"date": "2023-03-09",
"temperature": "16/27℃",
"weather": "多云",
"wid": {
"day": "01",
"night": "01"
},
"direct": "持續(xù)無風(fēng)向"
}
]
},
"error_code": 0
}
四、解析數(shù)據(jù)
一層數(shù)據(jù)解析?
我們先小試牛刀將 reason 中的 查詢成功! 解析出來先
JObject obj = (JObject)JsonConvert.DeserializeObject(strWebData);//將剛才一大串字符串轉(zhuǎn)換成一個(gè)大對象
string reason = obj["reason"].ToString();
Console.WriteLine(reason);
看一下打印的效果,可以成功將數(shù)據(jù)解析出來了
查詢成功!
?多層嵌套數(shù)據(jù)解析
我們嘗試將realtime里面的temperature數(shù)據(jù)解析下來看看,我們可以看到realtime的數(shù)據(jù)還是是保函在result里面的。直接暴力取,直搗黃龍,代碼如下
JObject obj = (JObject)JsonConvert.DeserializeObject(strWebData);//將剛才一大串字符串轉(zhuǎn)換成一個(gè)大對象
string reason = obj["reason"].ToString();
Console.WriteLine("reason = " + reason);
string temperature = obj["result"]["realtime"]["temperature"].ToString();
Console.WriteLine("temperature = " + temperature);
可以看一下打印的效果
reason = 查詢成功!
temperature = 25
?
但是有時(shí)候我們需要未來的天氣情況怎么辦,未來的天氣數(shù)據(jù)存在一個(gè)叫future的數(shù)組當(dāng)中,而且數(shù)組中的每個(gè)元素都是一個(gè)對象,這可怎么辦,技能升級》》》》
解析JSON里面的數(shù)組
下面就是一個(gè)表演的時(shí)間啦,獲取一下未來的日期為例
string strfuture = obj["result"]["future"].ToString();
//轉(zhuǎn)換成JArray格式
JArray jsonArr = (JArray)JsonConvert.DeserializeObject(strfuture);
JObject[] DataObj = new JObject[jsonArr.Count];//這兩種方法是一樣的效果
List<JObject> futureobj = new List<JObject>();//這兩種方法是一樣的效果
//將數(shù)組的每一個(gè)成員轉(zhuǎn)換成JObject格式,并存起來
for (int i = 0; i < jsonArr.Count; i++)
{
futureobj.Add((JObject)jsonArr[i]);//使用其中一種方法就行了
DataObj[i] = (JObject)jsonArr[i];//使用其中一種方法就行了
}
//打印一下未來5天的日期,每個(gè)數(shù)組成員都是一個(gè)對象,用下標(biāo)取引用對象
for(int i = 0; i < DataObj.Length; i++)
{
string date = DataObj[i]["date"].ToString();
Console.WriteLine(date);
}
打印效果如下,這樣我們就已經(jīng)結(jié)束了?
2023-03-05
2023-03-06
2023-03-07
2023-03-08
2023-03-09
最后JSON反序列化方法 和 JSON序列化方法如下
//反序列化json
JObject obj = (JObject)JsonConvert.DeserializeObject(strWebData);
//序列化json p是一個(gè)對象類 將對象P序列化成一個(gè)json字符串
string output = JsonConvert.SerializeObject(p);
其實(shí)很多知識都要反復(fù)去練習(xí)鞏固才能成為自己的技能,共勉?。?!文章來源:http://www.zghlxwxcb.cn/news/detail-656262.html
下一篇:對象序列化成json數(shù)據(jù)格式文章來源地址http://www.zghlxwxcb.cn/news/detail-656262.html
到了這里,關(guān)于C#請求訪問HTTP+JSON數(shù)據(jù)的解析的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!