在Windows手机上将json反序列化为对象c#
发布时间:2020-12-14 05:28:32 所属栏目:Windows 来源:网络整理
导读:我正在尝试将从Web服务器收到的json数据服务反序列化为对象.到目前为止,我刚刚建立了一个httpwebrequest,它从服务器获取json fromatted数据. public void DoHttpWebRequest(string url){ HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
我正在尝试将从Web服务器收到的json数据服务反序列化为对象.到目前为止,我刚刚建立了一个httpwebrequest,它从服务器获取json fromatted数据.
public void DoHttpWebRequest(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.BeginGetResponse(new AsyncCallback(onGetResponse),request); } public void onGetResponse (IAsyncResult asyncResult) { HttpWebRequest myRequest = (HttpWebRequest)asyncResult.AsyncState; HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(asyncResult); using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream())) { string results = httpwebStreamReader.ReadToEnd(); Dispatcher.BeginInvoke(() => textBlock5.Text = results); } myResponse.Close(); } 这将返回以下数据. {"BodyStyle":"Sports","ChassisNumber":19316,"Colour":"Ivory","Condition":"Showroom","Model":"Silver Wraith","Owners":[{"DateBought":"/Date(-207269643940+0100)/","DateSold":"/Date(-113297981580+0100)/","ID":651,"Owner":{"Address":null,"Decorations":null,"Email":"jvcuejnj.ldmfkiftvh@wx-sts.net","Forename":"Ismael","ID":637,"Mobile":"008547-4461","Surname":"Anderson","Telephone":"366892-9004","Title":"Mr"}}],"RegistrationNumber":"RB4107 ","Year":1909} 如何使用DataContractJsonSerializer将数据解析为具有以下类的对象? public class CarOwnershipRecord { public int? ID{ get; set; } public DateTime? DateBought{ get; set; } public DateTime? DateSold{ get; set; } public Person Owner{ get; set; } } public class Car { public string BodyStyle{ get; set; } public short? ChassisNumber{ get; set; } public string Colour{ get; set; } public string Condition{ get; set; } public string Model{ get; set; } public List<CarOwnershipRecord> Owners{ get; set; } public string RegistrationNumber{ get; set; } public short Year{ get; set; } } public class CarPhoto { public string RegistrationNumber{ get; set; } public byte[] Photo{ get; set; } // The Photo field contains the binary contents of an image file } 解决方法
这样的事情会给你正确的结果:
byte[] data = Encoding.UTF8.GetBytes(jsonString); MemoryStream memStream = new MemoryStream(data); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Car)); Car car = (Car) serializer.ReadObject(memStream); 虽然如果要跳过MemoryStream部分,可以直接从响应流反序列化 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- active-directory – 远程桌面连接被拒绝,因为用户帐户未被
- 当用户更改密码时,Windows计划任务失败的解决方法
- Windows – 从函数内部退出批处理脚本
- .net – WPF中的应用程序级快捷键
- windows-phone-7 – 刷新页面
- ActiveX Edge在Microsoft Edge中有替代品吗?
- windows-vista – 用于Vista 64位的RAM磁盘
- windows – 服务器重新启动后iSCSI目标卡住重新连接
- windows-server-2008 – 在KVM下安装Windows Server 2008的
- windows-7 – 有没有办法在不使用PowerShell或CIM Studio的
推荐文章
站长推荐
热点阅读