一、简单小结
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);