• C# 字符串转JSON


     一、简单小结

    C# 中 String 转 JSON

    var items = JsonConvert.DeserializeObject<class>(stringJSON);

    这里的 class 最好是直接定义 dto 来接收,不然不好变量和取值。

    二、使用案例

      json 字符串:

    [{"name":"温度A","id":"W1","measure":"°C","precision":0,"max":10,"min":0,"expression":"","map":"d1","value":"最大值"},{"name":"温度B","id":"W2","measure":"°C","precision":0,"expression":"","map":"d2","value":"最大值"},{"name":"拉力计A","id":"L1","measure":"Pa","precision":0,"expression":"","map":"d3","value":"最大值"}]
    

      类:StatTemplateStateDto

    public class StatTemplateStateDto
        {
            /// <summary>
            /// 映射标识
            /// </summary>
            public virtual String Id { get; set; }
    
            /// <summary>
            /// 名称
            /// </summary>
            public virtual String Name { get; set; }
    
            /// <summary>
            /// 单位
            /// </summary>
            public virtual String Measure { get; set; }
    
            /// <summary>
            /// 最小值
            /// </summary>
            public virtual String Precision { get; set; }
    
            /// <summary>
            /// 公式
            /// </summary>
            public virtual String Expression { get; set; }
    
            /// <summary>
            /// 映射
            /// </summary>
            public virtual String Map { get; set; }
    
            /// <summary>
            /// 特征值
            /// </summary>
            public virtual String Value { get; set; }
    
        }
    

     转换代码:

    var items = JsonConvert.DeserializeObject<StatTemplateStateDto[]>(input.StatTemplate.Content);
  • 相关阅读:
    AJAX从后台接收的字符串如何与前台字符串比较
    ojdbc6.jar导入maven依赖失败(亲测有效)
    IDEA怎样创建maven项目
    yum下载rpm包以及相关依赖包
    pip离线安装包
    Python的print的底层实现
    django查询集-17
    django的数据库操作-16
    django数据库迁移-15
    django定义模型类-14
  • 原文地址:https://www.cnblogs.com/gzbit-zxx/p/10831906.html
Copyright © 2020-2023  润新知