vlambda博客
学习文章列表

[C#] 上位机开发个人笔记 Json解析为Dictionary

Json分析器

在WebApi接口对接时,简单的接口可能不想写Model类,而又需要拿到Json结果中的值。或者接口返回具有多种模式,例如正常返回和异常返回不同。这些场景,都可以利用Json分析器JsonParser,把Json字符串解析为IDictionary<String,Object> 和 IList<Object> 构成的字典树。理论上,任意Json数据都可以用 IDictionary<String,Object> 和 IList<Object> 嵌套来表示。

源数据json格式。(主要是4个电能表数据)

Topic[ModbusTcp01] { "__startTime": "2022-03-17 18:05:20", "__name": "ModbusTcp01", "__description": "编号0001", "__config": "[ModbusTcp] [ModbusTcp01] [127.0.0.1:502]", "__success": 2322496, "__failed": 0, "__activeTime": "2022-03-24 11:24:49", "__captureSpendTime": 88, "__deviceStatus": true, "__requestEnable": true, "__onlineTime": "2022-03-17 18:05:22", "__failedMsg": "", "__alarmCount": { "AlarmCount": 12, "Hint": 0, "Warn": 0, "Error": 12, "Fatal": 0 }, "Ua": 2215, "Ub": 2164, "Uc": 2220, "Ia": 1203, "Ib": 1466, "Ic": 574, "In": 2217, "P": 2031, "Pa": 1091, "Pb": 1491, "Pc": 1361, "Q": 2207, "Qa": 1726, "Qb": 2220, "Qc": 2071, "S": 1947, "Sa": 983, "Sb": 506, "Sc": 1033, "PSum": 1353, "PFa": 2186, "PFb": 1473, "PFc": 1723, "Uab": 3782, "Ubc": 3714, "Uca": 3704, "Fa": 5031, "Fb": 4966, "Fc": 4907, "EpP": 696091, "EqP": 314, "EpN": 0, "EqN": 0, "a2": { "Ua": 2215, "Ub": 2164, "Uc": 2220, "Ia": 1203, "Ib": 1466, "Ic": 574, "In": 2217, "P": 2031, "Pa": 1091, "Pb": 1491, "Pc": 1361, "Q": 2207, "Qa": 1726, "Qb": 2220, "Qc": 2071, "S": 1947, "Sa": 983, "Sb": 506, "Sc": 1033, "PSum": 1353, "PFa": 2186, "PFb": 1473, "PFc": 1723, "Uab": 3782, "Ubc": 3714, "Uca": 3704, "Fa": 5031, "Fb": 4966, "Fc": 4907, "EpP": 696091, "EqP": 314, "EpN": 0, "EqN": 0 }, "a3": { "Ua": 2215, "Ub": 2164, "Uc": 2220, "Ia": 1203, "Ib": 1466, "Ic": 574, "In": 2217, "P": 2031, "Pa": 1091, "Pb": 1491, "Pc": 1361, "Q": 2207, "Qa": 1726, "Qb": 2220, "Qc": 2071, "S": 1947, "Sa": 983, "Sb": 506, "Sc": 1033, "PSum": 1353, "PFa": 2186, "PFb": 1473, "PFc": 1723, "Uab": 3782, "Ubc": 3714, "Uca": 3704, "Fa": 5031, "Fb": 4966, "Fc": 4907, "EpP": 696091, "EqP": 314, "EpN": 0, "EqN": 0 }, "a4": { "Ua": 2215, "Ub": 2164, "Uc": 2220, "Ia": 1203, "Ib": 1466, "Ic": 574, "In": 2217, "P": 2031, "Pa": 1091, "Pb": 1491, "Pc": 1361, "Q": 2207, "Qa": 1726, "Qb": 2220, "Qc": 2071, "S": 1947, "Sa": 983, "Sb": 506, "Sc": 1033, "PSum": 1353, "PFa": 2186, "PFb": 1473, "PFc": 1723, "Uab": 3782, "Ubc": 3714, "Uca": 3704, "Fa": 5031, "Fb": 4966, "Fc": 4907, "EpP": 696091, "EqP": 314, "EpN": 0, "EqN": 0 }}

转化为字典样式:

[C#] 上位机开发个人笔记 Json解析为Dictionary

有2层嵌套的

[C#] 上位机开发个人笔记 Json解析为Dictionary

[C#] 上位机开发个人笔记 Json解析为Dictionary

解析过程

[C#] 上位机开发个人笔记 Json解析为Dictionary

主要实现代码如下,便于自己复习。

代码说明,通过MQTT取得JSON数据,解析为字典数据便于页面显示。

var mqttPayload = JsonParser.Decode(e.Payload.ToStr());foreach (var dic in mqttPayload)    if (dic.Value.ToDictionary().Count() > 1)//通过count判断Json的嵌套深度 { var configs = dic.Value.ToDictionary(); foreach (var pair in configs) keyValuePairs.AddOrUpdate(e.Topic + "/" + dic.Key + "." + pair.Key, pair.Value.ToString(), (k, v) => keyValuePairs[e.Topic + "/" + dic.Key + "." + pair.Key] = pair.Value.ToString()); } else { keyValuePairs.AddOrUpdate(e.Topic + "/" + dic.Key, dic.Value.ToString(), (k, v) => keyValuePairs[e.Topic + "/" + dic.Key] = dic.Value.ToString());