嵌套数据的反序列化
我的第一次尝试和 JSON 反序列化,我陷入困境,只是想知道你是否可以提供帮助?
有以下 JSON
{
"summary":{
"pricing":{
"net":988,
"tax":13,
"gross":729
},
"status":{
"runningfor":29881175,
"stoppedfor":88805,
"idlefor":1298331744
}
}
}
我的 C# 代码
private void MakeRequest()
{
HttpWebRequest request = WebRequest.Create(Url) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
request.Credentials = new NetworkCredential(Username, Password);
request.Headers.Add(string.Format("App-Key: {0}", ApiKey));
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string resutls = reader.ReadToEnd();
Response.Write(resutls);
Status status = JSONHelper.JsonDeserialize<Status>(resutls);
Response.Write(status.RunningFor);
}
}
public class JSONHelper
{
/// <summary>
/// JSON Deserialization
/// </summary>
public static T JsonDeserialize<T>(string jsonString)
{
T obj = Activator.CreateInstance<T>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
ms.Close();
return obj;
}
}
[DataContractAttribute(Name = "status")]
public class Status
{
[DataMember(Name = "runningfor")]
public int RunningFor{ get; set; }
[DataMember(Name = "stoppedfor")]
public int StoppedFor{ get; set; }
[DataMember(Name = "idlefor")]
public int IdleFor{ get; set; }
}
,我只对状态结果感兴趣,没有其他任何兴趣。我做错了什么,因为它只为 RunningFor 返回 0。
提前致谢
My first attempt and JSON desrialization and I'm stuck, just wondering if you could help?
I have the following JSON
{
"summary":{
"pricing":{
"net":988,
"tax":13,
"gross":729
},
"status":{
"runningfor":29881175,
"stoppedfor":88805,
"idlefor":1298331744
}
}
}
here my c# code
private void MakeRequest()
{
HttpWebRequest request = WebRequest.Create(Url) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
request.Credentials = new NetworkCredential(Username, Password);
request.Headers.Add(string.Format("App-Key: {0}", ApiKey));
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string resutls = reader.ReadToEnd();
Response.Write(resutls);
Status status = JSONHelper.JsonDeserialize<Status>(resutls);
Response.Write(status.RunningFor);
}
}
public class JSONHelper
{
/// <summary>
/// JSON Deserialization
/// </summary>
public static T JsonDeserialize<T>(string jsonString)
{
T obj = Activator.CreateInstance<T>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
ms.Close();
return obj;
}
}
[DataContractAttribute(Name = "status")]
public class Status
{
[DataMember(Name = "runningfor")]
public int RunningFor{ get; set; }
[DataMember(Name = "stoppedfor")]
public int StoppedFor{ get; set; }
[DataMember(Name = "idlefor")]
public int IdleFor{ get; set; }
}
And I'm only interested in the status result nothing else at all. what am I doing wrong as it is only returning 0 for RunningFor.
Thanks In advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要反序列化为一个结构,该结构映射到您尝试反序列化的整个 JSON,而不仅仅是您想要的部分。对于您的情况,下面的代码显示了一种实现方法。
You need to deserialize into a structure which maps to the whole JSON you're trying to deserialize, not only to the part you want. In your case, the code below shows one way of doing it.
DataContractJsonSerializer 区分大小写。使用“跑步”。
The DataContractJsonSerializer is case-sensitive. Use "runningfor".