JSON反序列化的类 [英] Class for JSON deserialize
问题描述
我创建了一个RESTful WCF服务并将其托管在IIS中。当我查询我的服务'Get'方法时,我得到如下响应:
{GetDeviceDataResult:{FirstName:abc,Id :2,LastName:def}}
客户申请:
我正在尝试创建一个客户端应用程序,它将使用HTTPWebRequest查询服务器,这是我的代码片段,但我在反序列化json数据时将OutputResponse.deviceResult对象作为NULL。
Hi,
I created a RESTful WCF service and hosted it in IIS. When I query My service for 'Get' method I get the response as following:
{"GetDeviceDataResult":{"FirstName":"abc","Id":"2","LastName":"def"}}
Client Application:
I am trying to create a Client application, which will query the server using the HTTPWebRequest, here is my snippet but I am getting the OutputResponse.deviceResult object as NULL while deserializing the json data.
public void Start()
{
string url = "http://10.10.12.44/BLEService/BLEService.svc/GetDeviceData/2";
HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create(url);
GETRequest.Method = "GET";
string jsonString = string.Empty;
// System.Net.ServicePointManager.Expect100Continue = false;
using (HttpWebResponse response = GETRequest.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
jsonString = reader.ReadToEnd();
}
System.Web.Script.Serialization.JavaScriptSerializer jsSerializer =
new System.Web.Script.Serialization.JavaScriptSerializer { MaxJsonLength = 2147483647 };
ResponseData Outputresponse = jsSerializer.Deserialize<resp>(jsonString);
}
Here is my class for ResponseData
public class GetDeviceDataResult
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Id { get; set; }
}
public class ResponseData
{
public GetDeviceDataResult deviceResult { get; set; }
}
任何帮助将不胜感激。谢谢
Any help would be appreciated. Thanks
推荐答案
以下是答案:
我将GetDeviceDataResult的实例名称更改为GetDeviceDataResult 在ResponseData类中。以前我把它作为deviceResult
Here is the answer:
I changed the Instance name of GetDeviceDataResult to the "GetDeviceDataResult" in ResponseData class. Previously I had this as deviceResult
public class GetDeviceDataResult
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Id { get; set; }
}
public class ResponseData
{
public GetDeviceDataResult GetDeviceDataResult { get; set; }
}
这篇关于JSON反序列化的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!