本文介绍了将json响应字符串反序列化为类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到JSON响应字符串,即:



 [{Id:1,Name:name1 },{Id:2,姓名:name2},{Id:3,姓名:name3}] 





现在,如果我尝试将此字符串反序列化为类对象以及XmlDocument,则它无效。相反,我在Class对象和XmlDocument对象中都获得null值。



我尝试过:



我尝试将以下内容反序列化为一个类



使用(HttpWebResponse resp =(HttpWebResponse)req.GetResponse ())
{
if(resp.StatusCode == HttpStatusCode.OK)
{
StreamReader rd = new StreamReader(resp.GetResponseStream());
string str = rd.ReadToEnd(); //这里我可以看到响应JSON字符串
Student std = JsonConvert.DeserializeObject< Student>(str);
}
}





我尝试将以下内容反序列化为XmlDocument



使用(HttpWebResponse resp =(HttpWebResponse)req.GetResponse())
{
if(resp.StatusCode == HttpStatusCode.OK)
{
StreamReader rd = new StreamReader(resp.GetResponseStream());
string str = rd.ReadToEnd(); //这里我可以看到响应JSON字符串
XmlDocument xDoc = new XmlDocument();
xDoc = JsonConvert.DeserializeXmlNode(str);
}
}
解决方案




I am getting JSON response string, which is:

[{"Id":1,"Name":"name1"},{"Id":2,"Name":"name2"},{"Id":3,"Name":"name3"}]



now if I try to deserialize this string into class object and also in XmlDocument, its not working. Instead I am getting null value in both Class object and XmlDocument object.

What I have tried:

I tried the following for deserializing into a class

using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
  if (resp.StatusCode == HttpStatusCode.OK)
  {
    StreamReader rd = new StreamReader(resp.GetResponseStream());
    string str = rd.ReadToEnd(); //Here I can see the response JSON string
    Student std = JsonConvert.DeserializeObject<Student>(str);
  }
}



I tried the following for deserializing into XmlDocument

using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
  if (resp.StatusCode == HttpStatusCode.OK)
  {
    StreamReader rd = new StreamReader(resp.GetResponseStream());
    string str = rd.ReadToEnd(); //Here I can see the response JSON string
    XmlDocument xDoc = new XmlDocument();
    xDoc = JsonConvert.DeserializeXmlNode(str);
  }
}
解决方案




这篇关于将json响应字符串反序列化为类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 14:34
查看更多