http://james.newtonking.com/pages/json-net.aspx
private void frm_Load(object sender, EventArgs e)
{
String JSONString = "[{\"Name\":\"小明\",\"Url\":\"http://www.asp.com/\"},{\"Name\":\"小红\",\"Url\":\"http://www.abc.com/\"}]";
DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(List<Person>));
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(JSONString));
List<Person> list = (List<Person>)ds.ReadObject(ms);
ms.Close();
}
--------------------------------------------------------
这个问题的答案
[DataContractAttribute]
public class BAIDU {
[DataMember]
public int cur_page { get; set; }
[DataMember]
public int total_num { get; set; }
[DataMember]
public List<Data> data{get;set;}
public class Data {
public string title { get; set; }
public int thread_id { get; set; }
}
}
string json = "{\"cur_page\":1,\"total_num\":4,\"data\": [{\"title\":\"\\u4e50\\u5668\",\"thread_id\":111}, {\"title\":\"\\u4eca\\u65e5\",\"thread_id\":112}, {\"title\":\"\\u4eca\\u65e5\",\"thread_id\":113}, {\"title\":\"\\u4eca\\u65e5\",\"thread_id\":114}]}";
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(BAIDU));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) {
BAIDU baidu = (BAIDU)ser.ReadObject(ms);
//baidu 就是反序列化后的结果
ms.Close();
}
本文介绍了一个使用DataContractJsonSerializer进行JSON序列化和反序列化的实例,演示了如何将JSON字符串转换为C#对象列表的过程。通过具体的代码示例,展示了如何定义类结构以匹配JSON数据,并实现其反序列化。
2389

被折叠的 条评论
为什么被折叠?



