从动态{System.RuntimeType}变量解析json响应时获取错误

时间:2017-07-12 15:46:47

标签: json linq dynamic

我正在研究一些使用动态变量 jsonResponse 的代码。

dynamic jsonResponse = JsonConvert.DeserializeObject(response);

此变量包含json格式的酒店列表集合。我从这个集合中获得了一个新的变量 roomResponseList

var roomResponseList = jsonResponse["hotels"]["hotels"][rooms].roomResponseList;

我得到第一个房间细节到** JObject responseRateKeys **:

foreach (var roomByResponse in roomResponseList)
{
    JObject responseRateKeys = JObject.Parse(roomByResponse.ToString());
    var boardNameListByResponse = responseRateKeys.AsJEnumerable().AsEnumerable()
                                                   .Select(t => t["rates"]["boardName"].ToString().Trim())
                                                   .Distinct()
                                                   .ToList();
}

但是当我试图通过使用linq lambda从JObject获取任何项目列表时,我收到错误, “无法访问Newtonsoft.Json.Linq.JProperty上的子值。

roomByResponse =

的价值
{ "code": "DBL.KG-NM", "name": "DOUBLE KING BED NON SMOKING", "rates": [ { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|RO|IWH25|1~1~0||N@AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NRF", "rateType": "RECHECK", "net": "186.04", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "RO", "boardName": "ROOM ONLY", "cancellationPolicies": [ { "amount": "149.63", "from": "2017-07-14T03:29:00+05:30" } ], "rooms": 1, "adults": 1, "children": 0, "dailyRates": [ { "offset": 1, "dailyNet": "93.02" }, { "offset": 2, "dailyNet": "93.02" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|BB|IWB25|1~1~0||N@AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NOR", "rateType": "RECHECK", "net": "238.92", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "BB", "boardName": "BED AND BREAKFAST", "rooms": 1, "adults": 1, "children": 0, "dailyRates": [ { "offset": 1, "dailyNet": "119.46" }, { "offset": 2, "dailyNet": "119.46" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|RO|IWH25|2~2~1|2|N@AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NRF", "rateType": "RECHECK", "net": "372.06", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "RO", "boardName": "ROOM ONLY", "cancellationPolicies": [ { "amount": "299.25", "from": "2017-07-14T03:29:00+05:30" } ], "rooms": 2, "adults": 2, "children": 1, "childrenAges": "2", "dailyRates": [ { "offset": 1, "dailyNet": "186.03" }, { "offset": 2, "dailyNet": "186.03" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|BB|IWB25|2~2~1|2|N@AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NOR", "rateType": "RECHECK", "net": "477.84", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "BB", "boardName": "BED AND BREAKFAST", "rooms": 2, "adults": 2, "children": 1, "childrenAges": "2", "dailyRates": [ { "offset": 1, "dailyNet": "238.92" }, { "offset": 2, "dailyNet": "238.92" } ] } ] }

谢谢 Pravesh Singh

1 个答案:

答案 0 :(得分:0)

将linq更改为

responseRateKeys["rates"].AsJEnumerable().Select(t=>t["boardName"]).Distinct().ToList()