无法将类型“Newtonsoft.Json.Linq.JProperty”转换为“System.Collections.
本文关键字:转换 Collections System JProperty Json 类型 Newtonsoft Linq | 更新日期: 2023-09-27 18:25:13
var stringResult = {"reporting":{"default":{"Outpatient":8045376.0,"OutpatientPMPM":101.6472,"totalWorkersCompClaimsPaid":7718428.46,"totalWorkersCompClaimsPaidPMPM":97.5165,"totalMedicalPaidAmount":1.6883294E7,"totalMedicalPaidAmountPMPM":213.3076,"totalVisionClaimsPaid":2837.69,"totalVisionClaimsPaidPMPM":0.0359,"totalPharmacyPaidAmount":2.478251486E7,"totalPharmacyPaidAmountPMPM":313.1082,"totalDentalClaimsPaid":12271.67,"totalDentalClaimsPaidPMPM":0.155,"employeeMonths":35415.0,"memberMonths":79150.0,"subscribers":3009.0,"totalVendorPaidAmount":97034.0,"totalVendorPaidAmountPMPM":1.226,"Office":4285314.0,"OfficePMPM":54.1417,"Inpatient":4552604.0,"InpatientPMPM":57.5187,"members":6741.0,"totalHealthPlanClaimsPaid":4.949638068000001E7,"averageFamilySize":2.2403,"totalHealthPlanClaimsPaidPMPM":625.3492}}}
dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(stringResult);
float value = json.reporting.@default.totalPharmacyPaidAmount;
foreach (KeyValuePair<string, dynamic> page in json.reporting.@default)
{
var key = page.Key;
var totalHealthPlanClaimsPaid = page.Value.totalHealthPlanClaimsPaid;
var averageFamilySize = page.Value.averageFamilySize;
}
我在 foreach 循环中收到错误,因为
无法将类型"Newtonsoft.Json.Linq.JProperty"转换为 'System.Collections.Generic.KeyValuePair'.
在执行上述任务时,我做错了。
json.reporting.@default 返回 Newtonsoft.Json.Linq.JProperty,更改代码:
foreach (KeyValuePair<string, dynamic> page in json.reporting.@default.Children())
{
var key = page.Key;
var totalHealthPlanClaimsPaid = page.Value.totalHealthPlanClaimsPaid;
var averageFamilySize = page.Value.averageFamilySize;
}
文档阅读有关它的信息 http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JProperty.htm
@progpow说的是对的。还可以将结果集转换为字典并循环访问它。
Dictionary<string, dynamic> result = json.reporting.@default.ToObject<Dictionary<string,dynamic>>();
foreach (KeyValuePair<string,dynamic> page in result)
{
var key = page.Key;
var totalHealthPlanClaimsPaid = page.Value.totalHealthPlanClaimsPaid;
var averageFamilySize = page.Value.averageFamilySize;
}