解析格式1:
{ "code": 200, "message": "操作成功", "data": { "Id": "123456" }, "success": true }
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
public string CreateMessage(string url) { string ret = string.Empty; string consumerId = ""; try { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url); webReq.Method = "POST"; webReq.ContentType = "application/json"; webReq.Headers.Add("Authorization", "bearer ******"); Stream postData = webReq.GetRequestStream(); postData.Close(); HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse(); StreamReader sr = new StreamReader(webResp.GetResponseStream(), Encoding.UTF8); ret = sr.ReadToEnd();
//此处解析 JObject jsonObj = JObject.Parse(ret);
string code = jsonObj["code"].ToString();
if (code == "200") {
//拿到ID的值 consumerId = ((JObject)jsonObj["data"])["Id"].ToString(); } } catch (Exception ex) { //LogManager.LogInstance.WriteLog("时间:" + DateTime.Now + "/n 请求出错原因" + ex.ToString()); } return consumerId; }
解析格式2:
{ "code": 200, "message": "操作成功", "data": [ { "msgId": "123456", "msgType": "type1", "content": "{"eventId":"20161111","dateTime":"2021-06-17"}" }, { "msgId": "456789", "msgType": "type2", "content": "{"eventId":"20161112","dateTime":"2021-06-17"}" }
]
}
封装格式:
封装格式: { "ddCode": "001", "lists": [{ "Time": "2021-6-17 18:12:14", "Code": "123456", "Image": "****", }] }
public string GetWarningData(string consumerId) { string ret = string.Empty; try { string nUrl = "https://****/messages?consumerId=" + consumerId + "&autoCommit=true"; System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(nUrl); webReq.Method = "POST"; webReq.ContentType = "application/json"; webReq.Headers.Add("Authorization", "bearer *******"); //获取服务端返回 HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); ret = sr.ReadToEnd().Trim(); JObject jo = (JObject)JsonConvert.DeserializeObject(ret); string code = jo["code"].ToString(); #region log日志 //存储 拉取数据code值,通道ID,时间 warninfoManageservice.SaveLog(code, consumerId); #endregion if (code == "200") { JArray jar = JArray.Parse(jo["data"].ToString());
//如果封装多条数据,需要将faceobj和lists放在此处
//FaceMessage faceobj = new FaceMessage();
//faceobj.lists = new List<lists>(); for (var i = 0; i < jar.Count; i++) { JObject j = JObject.Parse(jar[i].ToString()); string msgId = j["msgId"].ToString(); JObject con = JObject.Parse(jar[i]["content"].ToString()); string dt = con["dateTime"].ToString();
//至此解析完成
//封装数据
//如果封装多条lists需要将faceobj和lists两个新的对象拿到for循环外面。
FaceMessage faceobj = new FaceMessage();
faceobj.lists = new List<lists>();
faceobj.ddCode = "001";
lists objlist = new lists(); //实例化JSON数组对象,用于添加JSON数组集合
objlist.Time =dt;
objlist.Code ="123456";
; faceobj.lists.Add(objlist); //添加JSON数组集合封装完成
string res = JsonConvert.SerializeObject(faceobj);//转成json数据
} } sr.Close(); } catch (Exception ex) { } return ret; }
创建model
public class FaceMessage { public string ddCode{ get; set; } public List<lists> lists { get; set; } } public class lists { public string Time { get; set; } public string Code { get; set; } public string Image { get; set; } }