在C#中反序列化json数据列表

时间:2018-07-30 16:14:20

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

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);
}

此处为字符串json:

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

可以,但是,如何反序列化列表中的数据? 预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

List<information> informationList = JsonConvert.DeserializeObject<List<information>>(json);

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