码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET JSON数据转实体类方式

时间:2018-03-08 17:22:01      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:文档   实现   ext   style   scripts   之间   pat   serial   serialize   

实体类

 public class FlieList
    {

            public string file_unid { get; set; }
     
            public string file_name { get; set; }

            public object file_md5 { get; set; }

            public string file_path { get; set; }

            public string file_ext { get; set; }

            public string file_createtime { get; set; }
     
            public string file_size { get; set; }
   
            public string file_state { get; set; }
        
            public string file_save_type { get; set; }
 
            public string file_belongto { get; set; }
}

  json数据单条:

{
        "file_unid":"20170724-7F2A0BE3607DFEE0E134-11",
        "file_name":"文档.doc",
        "file_md5":null,
        "file_path":"/fileark/3496dd7b36ce3b6ef78f33fe64c2ea10.doc",
        "file_ext":"doc",
        "file_createtime":"2017-07-24 10:46:41",
        "file_size":"24064",
        "file_state":"0",
        "file_save_type":"0",
        "file_belongto":"EC388D0A7184CC4EFCC005E8E97B10BB"
    }

  json数据多条:

[
    {
        "file_unid":"20170724-7F2A0BE3607DFEE0E134-11",
        "file_name":"文档.doc",
        "file_md5":null,
        "file_path":"/fileark/3496dd7b36ce3b6ef78f33fe64c2ea10.doc",
        "file_ext":"doc",
        "file_createtime":"2017-07-24 10:46:41",
        "file_size":"24064",
        "file_state":"0",
        "file_save_type":"0",
        "file_belongto":"EC388D0A7184CC4EFCC005E8E97B10BB"
    },
    {
        "file_unid":"20170724-7223CE403B7772D16304-11",
        "file_name":"文档1.doc",
        "file_md5":null,
        "file_path":"/fileark/54e9a264ffe764b0848686d703368e44.doc",
        "file_ext":"doc",
        "file_createtime":"2017-07-24 10:46:28",
        "file_size":"22528",
        "file_state":"0",
        "file_save_type":"0",
        "file_belongto":"EC388D0A7184CC4EFCC005E8E97B10BB"
    }
]

  注意:实体类要与json里面字段相对应,实体没有json那么多字段也可以,能跟json匹配的实体类字段会一一赋值。

具体实现:

  转单个实体类的实现

1.适用范围 .framework2.0~ .framework3.5之间

 JavaScriptSerializer js = new JavaScriptSerializer();              
FlieList file= js.Deserialize<FlieList >(jsonString);  

2. 适用范围.framework3.5以下版本不适用 

FlieList file= (FlieList)JsonConvert.DeserializeObject(data, typeof(FlieList));

 转集合实体类的实现

1.适用范围 .framework2.0~ .framework3.5之间

 JavaScriptSerializer js = new JavaScriptSerializer();
 List<FlieList> file = js.Deserialize<List<FlieList>>(jsonString);

2. 适用范围.framework3.5以下版本不适用 

List<FlieList> user = (List<FlieList>)JsonConvert.DeserializeObject(data, typeof(List<FlieList>));

  

ASP.NET JSON数据转实体类方式

标签:文档   实现   ext   style   scripts   之间   pat   serial   serialize   

原文地址:https://www.cnblogs.com/jiangxifanzhouyudu/p/8528972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!