【问题标题】:Deserialize json data list in C#在 C# 中反序列化 json 数据列表
【发布时间】:2018-07-30 16:14:20
【问题描述】:
public class information
{
     public string name { get; set; }
     public string surname { get; set; }
}
public class JsonFormat
{
     public IList<information> information { get; set; }
}
protected void gettextbox_Click(object sender, EventArgs e)
{
     JsonFormat newjson = new JsonFormat();
     List<information> p = new List<information>();
     information add1 = new information { name = textbox1.Text , surname = textbox2.Text };
     information add2 = new information { name = textbox3.Text, surname = textbox4.Text };
     p.Add(add1);
     p.Add(add2);
     newjson.information = p;
     string json = JsonConvert.SerializeObject(newjson);
}

这里是字符串:

"{\"information\":[{\"name\":\"data1\",\"surname\":\"data2\"}, \"name\":\"data3\",\"surname\":\"data4\"}]}"

这里没问题,但是如何反序列化列表中的数据? 提前感谢您的帮助。

【问题讨论】:

标签: c# asp.net json serialization json-deserialization


【解决方案1】:
List<information> informationList = JsonConvert.DeserializeObject<List<information>>(json);

https://www.newtonsoft.com/json/help/html/DeserializeObject.htm

【讨论】:

  • 好的,信息信息=JsonConvert.DeserializeObject(json); information.name --> 哪个收入,data1 或 data3 ?
  • 我注意到您有信息对象列表,因此您必须反序列化为 List
  • 另外,而不是直接序列化信息列表为什么你在做newjson.information = p;然后string json = JsonConvert.SerializeObject(newjson);
猜你喜欢
  • 1970-01-01
  • 2013-02-13
  • 1970-01-01
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
  • 1970-01-01
  • 2016-05-14
  • 1970-01-01
相关资源
最近更新 更多