本文介绍了将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); } }解决方案
一个 JSON数组用学生数组解析它 学生[]学生= JsonConvert.DeserializeObject< Student []>(json); foreach(学生在学生) { int id = student.Id; string name = student.Name; }
确保学生类看起来像这样
class学生 { public int Id {get;组; } public string Name {get;组; } }
要扩展Karthik Bangalore解决方案,在C#中使用JSON& VB [ ^ ]是为Q& A这样的问题编写的CodeProject文章。它涵盖了工具,包括代码生成,帮助程序类和可以下载和运行的完整工作示例。
为了进一步扩展Karthik Bangalore的解决方案,在List<时你不需要使用数组; T>是的,所以这就是我要做的事情: 使用 Newtonsoft。 JSON; 使用 System.Collections.Generic; 命名空间 WorkingWithJson { public class Person { public int Id { get ; set ; } public string 名称{ get ; set ; } } public class Working_with_json { public static void DeserializeJSON () { // 我们的json字符串 string json = @ [{Id:1 名称 : NAME1 },{ ID 为:2 名称 : NAME2 },{ ID :3 名称 : NAME3 }]; // 反序列化为Person类型列表 List< ;人> people = JsonConvert.DeserializeObject< List< Person>>(json); // 然后可以检索 系统。 Console.WriteLine(
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); } } 解决方案 Its a JSON array so parse it with Student Array Student[] Students = JsonConvert.DeserializeObject<Student[]>(json); foreach (Student student in Students) { int id = student.Id; string name = student.Name; }ensure the Student class looks like this
class Student { public int Id { get; set; } public string Name { get; set; } } To expand on Karthik Bangalore solution, Working with JSON in C# & VB[^] is a CodeProject article written for Q&A questions like this one. It covers tools, including code generation, helper classes, and full working samples that you can download and run.To further expand on Karthik Bangalore's solution, you dont need to use an array when List<t> is available so here's what I would do: using Newtonsoft.Json; using System.Collections.Generic; namespace WorkingWithJson { public class Person { public int Id { get; set; } public string Name { get; set; } } public class Working_with_json { public static void DeserializeJSON() { // our json string string json = @"[{""Id"":1,""Name"":""name1""},{""Id"":2,""Name"":""name2""},{""Id"":3,""Name"":""name3""}]"; // deserializing to a list of type Person List<Person> people = JsonConvert.DeserializeObject<List<Person>>(json); // which can then be retrieved thus System.Console.WriteLine(更多推荐
将json响应字符串反序列化为类
发布评论