[{"userId":17689926,"uin":17848887,"userName":"\u7dc8\u8ae8d\u0451\u857e\u857e","headPic":"http:\/\/qlogo4.store.qq.com\/qzonelogo\/17848887\/1\/1258952639","yellowlevel":"4","yellowstatus":1,"exp":108809,"money":474088,"pf":1},{"userId":323423231,"uin":323170725,"userName":"\u8303\u6c38\u82b3","headPic":"http:\/\/imgcache.qq.com\/qzone_v4\/client\/userinfo_icon\/5001.gif","yellowlevel":"0","yellowstatus":0,"exp":21697,"money":341296,"pf":1}]在c#中如何处理这种多条记录的json字符串,把它绑定到dataGridView中,请高手简单写一下代码啊,我在网上找了半天,都是把一条json字符串中的一条记录序列化一个对象中,如何处理一条json中的多条记录呢?
下面是个json对象。
public class JsonObject{
public string name{ get; set; }
public string sex{ get; set; }
public string age{ get; set; }
public string note{ get; set; }
}
JsonObject jsonObj=new JsonObject(); jsonObj.name="sam";jsonObj.sex="男";
string jsonStr=JsonTools.ObjectToJson(JsonObject);
JsonTools.JsonToObject(jsonStr,jsonObj);
多条记录就传json对象数组。例如:JsonObject[] jsonObjArr=new JsonObject[10];
给数组元素赋值后再使用jsonStr=JsonTools.ObjectToJson(jsonObjArr);即可序列化为json字符串
public class QQfriendList
{
public QQfriendList() { } [DataMember(Name = "TQQfriend")]
public QQfriend[] TQQfriend { get; set; }
} [DataContract]
public class QQfriend
{
[DataMember(Name = "userId")]
public string userId { get; set; }
[DataMember(Name = "uin")]
public string uin { get; set; }
[DataMember(Name = "userName")]
public string userName { get; set; }
[DataMember(Name = "headPic")]
public string headPic { get; set; }
[DataMember(Name = "yellowlevel")]
public string yellowlevel { get; set; }
[DataMember(Name = "yellowstatus")]
public string yellowstatus { get; set; }
[DataMember(Name = "exp")]
public string exp { get; set; }
[DataMember(Name = "money")]
public string money { get; set; }
[DataMember(Name = "pf")]
public string pf { get; set; }
}
--------------------------------------------------------代码
MemoryStream mStream = new MemoryStream(str);
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(QQfriendList));
friendlist = (QQfriendList)serializer.ReadObject(mStream); foreach (QQfriend tt in friendlist.TQQfriend)
{
richTextBox1.AppendText(string.Format("{0}userName\r\n", tt.userName));
}
为什么friendlist.TQQfriend是空的啊!!急!!
{"TQQfriend":[{"userId":"000","uin":null,"userName":null,"headPic":null,"yellowlevel":null,"yellowstatus":null,"exp":null,"money":null,"pf":null},{"userId":"001","uin":null,"userName":null,"headPic":null,"yellowlevel":null,"yellowstatus":null,"exp":null,"money":null,"pf":null}]}