LOGO 首页 OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 技术文档 其他文档  
 
网站管理员

C#.net 解析并提取嵌套格式的 JSON 中的数据

admin
2026年9月3日 11:25 本文热度 59

JSON格式文件如下:

我们是要取出msgJsoncontent里面GeneralReportInfo下serviceData中的totalUseValue数据

{

    "responseCode": "100000",

    "message": "运行正确",

    "result": {

        "pageNum": 1,

        "pageSize": 10,

        "size": 10,

        "startRow": 1,

        "endRow": 10,

        "total": 27,

        "pages": 3,

        "list": [

            {

                "id": "7bcba4b7508a433db7664ec9f60fef2a",

                "registTime": "2019-07-01 23:49:17",

                "updateTime": "2019-07-01 23:49:48",

                "gmId": "9999999000008888",

                "concentratorNum": "",

                "deviceType": "",

                "subdeviceType": "",

                "anysType": 0,

                "port": 0,

                "ip": "",

                "msgJsoncontent": "{\"identifier\":\"9000008888\",\"hasMore\":0,\"mid\":598,\"msgType\":\"deviceReq\",\"data\":[{\"serviceId\":\"GeneralReportInfo\",\"serviceData\":{\"dfCount\":\"0\",\"dfId\":\"0\",\"connType\":\"0\",\"deviceType\":\"2\",\"subDeviceType\":\"12\",\"companyNo\":\"999999\",\"meterType\":\"0000\",\"dataType\":\"00\",\"totalUseValue\":\"7.90\",\"recentFreezeValue\":\"7.90\",\"deviceStatus\":{\"loseTrack\":\"0\",\"valueLack\":\"0\",\"leakAlarm\":\"0\",\"remoteCloseValve\":\"1\",\"vibrationStop\":\"0\",\"buttonMode\":\"0\",\"deviceLock\":\"0\",\"isAccount\":\"1\",\"breakLine\":\"0\",\"singleCount\":\"0\",\"magneticInterference\":\"0\",\"superFlow\":\"0\",\"factoryMode\":\"1\",\"pressureLack\":\"0\",\"valveState\":\"1\"},\"deviceTime\":\"20190701T234910Z\",\"voltage\":\"4.91\",\"identityCode\":\"89860317492039663768\",\"moduleIMEI\":\"863703038784190\",\"signalIntensity\":\"2\",\"signalStrength\":-1194,\"signalPower\":-1088,\"signalCellID\":163869138,\"signalECL\":1,\"signalSNR\":7,\"signalPCI\":295,\"linkQuality\":-136,\"softWareVer\":\"9\",\"firmwareVer\":\"R2027\"}},{\"serviceId\":\"FreezeDataReportInfo\",\"serviceData\":{\"dfCount\":\"0\",\"dfId\":\"0\",\"connType\":\"0\",\"deviceType\":\"2\",\"subDeviceType\":\"12\",\"companyNo\":\"999999\",\"freezeCount\":\"0\",\"freezeDatas\":null}}]}",

                "msgContent": "BB01025602080C9999999000008888000000006000A8B670678C9E3DC24A5CF51A67E0003E8C6F3CF513378103CF2B221B965DCA91F5DD1A7834337C1FA61DE71B04CC54F916701A5A77CF0544A2FEACA2DD6942BBB9718B7CDF5EAF5B2E63A5FE28E3794644F3EEA39D6CAD30A08199991C43DC25626C",

                "hasTestHang": 0,

                "callbackResult": 1,

                "msgDeviceType": 0,

                "linkType": 0

            },

            {

                "id": "e33b54d057c34f10bfecfe55d2e9684d",

                "registTime": "2019-07-01 15:49:11",

                "updateTime": "2019-07-01 15:49:12",

                "gmId": "9999999000008888",

                "concentratorNum": "",

                "deviceType": "",

                "subdeviceType": "",

                "anysType": 0,

                "port": 0,

                "ip": "",

                "msgJsoncontent": "{\"identifier\":\"9000008888\",\"hasMore\":0,\"mid\":586,\"msgType\":\"deviceReq\",\"data\":[{\"serviceId\":\"GeneralReportInfo\",\"serviceData\":{\"dfCount\":\"0\",\"dfId\":\"0\",\"connType\":\"0\",\"deviceType\":\"2\",\"subDeviceType\":\"12\",\"companyNo\":\"999999\",\"meterType\":\"0000\",\"dataType\":\"00\",\"totalUseValue\":\"7.90\",\"recentFreezeValue\":\"7.90\",\"deviceStatus\":{\"loseTrack\":\"0\",\"valueLack\":\"0\",\"leakAlarm\":\"0\",\"remoteCloseValve\":\"1\",\"vibrationStop\":\"0\",\"buttonMode\":\"0\",\"deviceLock\":\"0\",\"isAccount\":\"1\",\"breakLine\":\"0\",\"singleCount\":\"0\",\"magneticInterference\":\"0\",\"superFlow\":\"0\",\"factoryMode\":\"1\",\"pressureLack\":\"0\",\"valveState\":\"1\"},\"deviceTime\":\"20190701T154905Z\",\"voltage\":\"5.39\",\"identityCode\":\"89860317492039663768\",\"moduleIMEI\":\"863703038784190\",\"signalIntensity\":\"3\",\"signalStrength\":-1139,\"signalPower\":-1069,\"signalCellID\":80010836,\"signalECL\":1,\"signalSNR\":93,\"signalPCI\":183,\"linkQuality\":-111,\"softWareVer\":\"9\",\"firmwareVer\":\"R2027\"}},{\"serviceId\":\"FreezeDataReportInfo\",\"serviceData\":{\"dfCount\":\"0\",\"dfId\":\"0\",\"connType\":\"0\",\"deviceType\":\"2\",\"subDeviceType\":\"12\",\"companyNo\":\"999999\",\"freezeCount\":\"1\",\"freezeDatas\":[{\"freezeDate\":\"20190701T150000Z\",\"freezeValue\":\"7.90\"}]}}]}",

                "msgContent": "BB01024A02080C9999999000008888000000006000544E03707B118B43F50857CFED37915A97599E43DD2E90FDD58B0D41820A81DD7390E567D81CD14E5ACF7330A1238A89141DB5F37AD1C70876BE7DA16F50BB85FA930EA0E8D1DDABFB8F91528D762A55B706CFC565D19B8C75B333D6FFE81F7FAB4E",

                "hasTestHang": 0,

                "callbackResult": 1,

                "msgDeviceType": 0,

                "linkType": 0

            }

        ],

        "firstPage": 1,

        "prePage": 0,

        "nextPage": 2,

        "lastPage": 3,

        "isFirstPage": true,

        "isLastPage": false,

        "hasPreviousPage": false,

        "hasNextPage": true,

        "navigatePages": 8,

        "navigatepageNums": [1, 2, 3]

    },

    "args": ""

}

第一步:

responsecontent为上文的文件

将JSON字符串JObject格式化:JObject job = JObject.Parse(responsecontent);

第二步:

将list里面的数据Jarray化: JArray array2 = JArray.Parse(job["result"]["list"].ToString());

循环array2得到我们想要的数据,命名比较随意请见谅,以下是具体的解析代码:

for (int j = 0; j < array2.Count; j++)

{

    dr = dt.NewRow();

    JObject jObject = JObject.Parse(array2[j].ToString());

    string ss = jObject["msgJsoncontent"].ToString();

    JObject js = JObject.Parse(ss);

    string sss = js["data"].ToString();

    JArray array = JArray.Parse(sss);

    // 通过LINQ表达式选择data里面serviceId=GeneralReportInfo再获取下面的totalUseValue值

    foreach (var item in array.Where(a => a["serviceId"].ToString() == "GeneralReportInfo"))

    {

        JObject a = JObject.Parse(item["serviceData"].ToString());

        string totalUseValue = a["totalUseValue"].ToString(); // 此处我们就得到了我们想要的数据

    }

}


该文章在 2026/9/3 11:58:09 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2026 ClickSun All Rights Reserved  粤ICP备13012886号-2  粤公网安备44030602007207号