winform处理{ "msg": "success", "code": 0, "data": [ { "entName": "江苏快达农化股份有限公司总部", "type": "废水", "pointName": "快达(二期)", "monitorName": "流量", "monitorData": 0, "unit": "L/S", "monitorTime": "2023-05-31 21:13:10" }, { "entName": "江苏快达农化股份有限公司总部", "type": "废水", "pointName": "快达(二期)", "monitorName": "pH值", "monitorData": 8.05, "unit": null, "monitorTime": "2023-05-31 21:13:10" }, { "entName": "江苏快达农化股份有限公司总部", "type": "废水", "pointName": "快达(二期)", "monitorName": "COD", "monitorData": 195.4, "unit": "mg/L", "monitorTime": "2023-05-31 21:13:10" } ] }并找到"type":"废水"且"pointName":"快达(二期)"且"monitorName":"COD"的"monitorData"的值由label显示
时间: 2023-07-03 20:24:02 浏览: 98
基于C#(WinForm)设计火柴棍移动【100011663】
你可以使用 C# 的 Json.NET 库来解析 JSON 数据并从中提取所需信息,然后将 "monitorData" 的值显示在 Label 控件上。以下是一个简单的示例代码:
```csharp
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
// ...
string json = @"{""msg"":""success"",""code"":0,""data"":[{""entName"":""江苏快达农化股份有限公司总部"",""type"":""废水"",""pointName"":""快达(二期)"",""monitorName"":""流量"",""monitorData"":0,""unit"":""L/S"",""monitorTime"":""2023-05-31 21:13:10""},{""entName"":""江苏快达农化股份有限公司总部"",""type"":""废水"",""pointName"":""快达(二期)"",""monitorName"":""pH值"",""monitorData"":8.05,""unit"":null,""monitorTime"":""2023-05-31 21:13:10""},{""entName"":""江苏快达农化股份有限公司总部"",""type"":""废水"",""pointName"":""快达(二期)"",""monitorName"":""COD"",""monitorData"":195.4,""unit"":""mg/L"",""monitorTime"":""2023-05-31 21:13:10""}]}";
JObject obj = JObject.Parse(json);
JArray data = (JArray)obj["data"];
foreach (JToken token in data)
{
string type = (string)token["type"];
string pointName = (string)token["pointName"];
string monitorName = (string)token["monitorName"];
double monitorData = (double)token["monitorData"];
if (type == "废水" && pointName == "快达(二期)" && monitorName == "COD")
{
label1.Text = monitorData.ToString();
break;
}
}
```
这段代码将 JSON 数据解析为 JObject 对象,然后提取 "data" 字段的值并遍历其中的元素。对于每个元素,我们提取 "type"、"pointName"、"monitorName" 和 "monitorData" 字段的值,并检查它们是否符合要求。如果符合要求,就将 "monitorData" 的值显示在 Label 控件上,并退出循环(因为我们只需要显示一个值)。
阅读全文