[{"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中的多条记录呢?

解决方案 »

  1.   

    这个网址写了json的序列化:http://blog.zol.com.cn/1161/article_1160035.html
    下面是个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字符串
      

  2.   

     [DataContract]
        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是空的啊!!急!!
      

  3.   

    你的str格式不对,应该为:
    {"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}]}
      

  4.   

    你可以序列化一个对象后得到str,然后接着使用这个str反序列化,就不会出现序列化的str,与要反序列的str不一致了,希望你的问题是这样的。