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'。
在执行上述任务时我做错了。
答案 0 :(得分:2)
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
答案 1 :(得分:2)
@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;
}