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

      

  • 相关阅读:
    Chrome扩展开发之一——Chrome扩展的文件结构
    Chrome扩展开发(Gmail附件管理助手)系列之〇——概述
    Springfox与swagger的整合使用
    Maven的简单使用
    关于接口功能自动化的思考
    发版流程优化备忘录
    Aho-Corasick 自动机 学习笔记
    Luogu P1495 曹冲养猪
    Luogu P2670 【扫雷游戏】
    1.1 整除
  • 原文地址:https://www.cnblogs.com/jiangxifanzhouyudu/p/8528972.html
Copyright © 2020-2023  润新知