强大的json字符串对象,转化为object对象

1、添加dll(C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Data.DataSetExtensions.dll)

2、转化方法

//将json格式字符转source化为object对象
public static object JsonStrToObject<T>(string source)
        {             
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(source)))
            {
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
                return serializer.ReadObject(ms);
            }
        }
View Code

此方法能够转换嵌套的json格式对象。

示例:

json格式字符串 “{"statuses":[{"created_at":"Fri May 17 16:09:25 +0800 2013","id":3578977384167458,"mid":"3578977384167458","idstr":"3578977384167458","text":"分享 徐佳莹 的歌曲《绿洲》 http://t.cn/zWWCVCF(分享自 @虾米音乐)","source":"<a href=\"http://app.weibo.com/t/feed/6coOUz\" rel=\"nofollow\">虾米音乐</a>","favorited":false,"truncated":false,"in_reply_to_status_id":"","in_reply_to_user_id":"","in_reply_to_screen_name":"","pic_urls":[{"thumbnail_pic":"http://ww1.sinaimg.cn/thumbnail/5675d256jw1e4rczhqc6uj214h18f0yo.jpg"}],"thumbnail_pic":"http://ww1.sinaimg.cn/thumbnail/5675d256jw1e4rczhqc6uj214h18f0yo.jpg","bmiddle_pic":"http://ww1.sinaimg.cn/bmiddle/5675d256jw1e4rczhqc6uj214h18f0yo.jpg","original_pic":"http://ww1.sinaimg.cn/large/5675d256jw1e4rczhqc6uj214h18f0yo.jpg","geo":null,"user":{"id":1450562134,"idstr":"1450562134","screen_name":"听雨花生","name":"听雨花生","province":"35","city":"1","location":"福建 福州","description":"人世多愁,自在几人能够。独倚高楼, 总有人高歌相候。","url":"","profile_image_url":"http://tp3.sinaimg.cn/1450562134/50/22819836445/1","profile_url":"u/1450562134","domain":"","weihao":"","gender":"m","followers_count":74,"friends_count":86,"statuses_count":470,"favourites_count":6,"created_at":"Wed Jul 21 15:09:43 +0800 2010","following":false,"allow_all_act_msg":false,"geo_enabled":true,"verified":false,"verified_type":-1,"remark":"","allow_all_comment":true,"avatar_large":"http://tp3.sinaimg.cn/1450562134/180/22819836445/1","verified_reason":"","follow_me":false,"online_status":1,"bi_followers_count":22,"lang":"zh-cn","star":0,"mbtype":0,"mbrank":0,"block_word":0},"reposts_count":1,"comments_count":0,"attitudes_count":0,"mlevel":0,"visible":{"type":0,"list_id":0}}],"hasvisible":false,"previous_cursor":0,"next_cursor":0,"total_number":1}”

 

转化成的对象:

public class PublicWeibo
    {
        public List<Statuses> statuses { get; set; }          
        public string previous_cursor { get; set; }        
        public string next_cursor { get; set; }        
        public string total_number { get; set; }        
    }
    public class Statuses 
    {
        public string created_at { get; set; }
        public string id { get; set; }
        public string text { get; set; }
        public string source { get; set; }
        public string favorited { get; set; }
        public User user { get; set; }
    }
    public class User 
    { 
        public string id {get;set;}
        public string screen_name {get;set;}
        public string name {get;set;}
        public string province {get;set;}
        public string city {get;set;}
        public string location {get;set;}
        public string description {get;set;}
        public string url {get;set;}
        public string profile_image_url {get;set;}
        public string domain {get;set;}
        public string gender {get;set;}
        public string followers_count{get;set;}
    }
View Code

属性太多,并没有每个属性都表示下来,可以发现,在对象中没有列出的属性并不会影响已有属性。

 

posted @ 2013-05-17 16:04  海藻  阅读(1125)  评论(0编辑  收藏  举报