.net 解析嵌套JSON

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);
                            //通过lamda表达式选择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();//此处我们就得到了我们想要的数据 
                                
                            } 
                         }
View Code

 

posted @ 2019-08-01 15:24  我本梁人  阅读(619)  评论(0编辑  收藏  举报