c# 动态获取http通过post传来的json数据
//获取http通过post传来的json数据
using Newtonsoft.Json;
public HttpResponseMessage Query()
{
var request=System.Web.HttpContext.Current.Request;
byte[] requestData=new byte[request.InputStream.Length];
request.InputStream.Read(requestData,0,(int)request.InputStream.Length);
var jsonData=Encoding.UTF8.GetString(requestData);
//转化成json数据
dynamic m=jsonData.ToObject<dynamic>();
string MeterAddr=string.Empty;
string StartTime=string.Empty;
string EndTime=string.Empty;
//获取json数据的字段值
MeterAddr=m.meterid;
StartTime=m.StartTime;
EndTime=m.EndTime;
}
//返回json数据
public static T ToObject<T>(this string Json)
{
return Json==null?default(T):JsonConvert.DeserializeObject<T>(Json)
}