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


    实体类

     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>));
    

      

  • 相关阅读:
    Hibernate的注释该如何使用?每一个注释代表什么意思?
    J2SE总结(一)-------容器
    解决hibernate向mysql插入中文乱码问题(更改MySQL字符集)
    android程序员成长路径的思考
    Fragment总结
    onCreateView的一个细节--Fragment
    屏幕适配
    表驱动法3
    表驱动法2
    表驱动法1
  • 原文地址:https://www.cnblogs.com/jiangxifanzhouyudu/p/8528972.html
Copyright © 2020-2023  润新知