快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

强大的json字符串对象,转化为object对象-创新互联

1、添加dll(C:Program FilesReference AssembliesMicrosoftFramework.NETFrameworkv4.0ProfileClientSystem.Data.DataSetExtensions.dll)强大的json字符串对象,转化为
object对象

2、转化方法

成都创新互联是一家专注于网站设计制作、做网站与策划设计,安新网站建设哪家好?成都创新互联做网站,专注于网站建设十余年,网设计领域的专业建站公司;建站业务涵盖:安新等地区。安新做网站价格咨询:13518219792
//将json格式字符转source化为object对象public static object JsonStrToObject(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":"虾米音乐","favorited":false,"truncated":false,"in_reply_to_status_id":"","in_reply_to_user_id":"","in_reply_to_screen_name":"","pic_urls":[{"thumbnail_pic":"/upload/otherpic40/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 { 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

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


新闻名称:强大的json字符串对象,转化为object对象-创新互联
分享URL:http://6mz.cn/article/dhpijp.html

其他资讯