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\"}]}"
可以,但是,如何反序列化列表中的数据? 预先感谢您的帮助。
答案 0 :(得分:0)
List<information> informationList = JsonConvert.DeserializeObject<List<information>>(json);
https://www.newtonsoft.com/json/help/html/DeserializeObject.htm