C# 访问的JArray值的键值无效

C# 访问的JArray值的键值无效,c#,json,json.net,C#,Json,Json.net,我在尝试挖掘json数据时遇到以下错误 访问的JArray值具有无效键值:“RateInfos”。应为数组位置索引 任何人都可以帮助解决这个问题,我正在尝试获取currentAllocation:3的值,它是从提供的json底部向上15行 customerSessionId: "0ABAA82A-4307-A913-E172-4D238D901107", numberOfRoomsRequested: 1, moreResultsAvailable: true, cach

我在尝试挖掘json数据时遇到以下错误

访问的JArray值具有无效键值:“RateInfos”。应为数组位置索引

任何人都可以帮助解决这个问题,我正在尝试获取currentAllocation:3的值,它是从提供的json底部向上15行

customerSessionId: "0ABAA82A-4307-A913-E172-4D238D901107",
    numberOfRoomsRequested: 1,
    moreResultsAvailable: true,
    cacheKey: "-73d4307a:13e174d238d:-10fe",
    cacheLocation: "10.186.168.42:7301",
    cachedSupplierResponse: {
        @matchedLocale: "true",
        @matchedCurrency: "true",
        @tpidUsed: "5101",
        @otherOverheadTime: "1",
        @candidatePreptime: "98",
        @supplierResponseTime: "498",
        @supplierResponseNum: "1",
        @supplierRequestNum: "207",
        @cachedTime: "0",
        @supplierCacheTolerance: "NOT_SUPPORTED"
    },
    HotelList: {
        @activePropertyCount: "224",
        @size: "1",
        HotelSummary: {
            @ubsScore: "15815285",
            @order: "0",
            hotelId: 161395,
            name: "Larkspur Landing Bellevue - An All-Suite Hotel",
            address1: "15805 Se 37th St",
            city: "Bellevue",
            stateProvinceCode: "WA",
            postalCode: 98006,
            countryCode: "US",
            airportCode: "SEA",
            supplierType: "E",
            propertyCategory: 1,
            hotelRating: 3,
            confidenceRating: 95,
            amenityMask: 7823362,
            tripAdvisorRating: 4,
            tripAdvisorReviewCount: 81,
            tripAdvisorRatingUrl: "http://www.tripadvisor.com/img/cdsi/img2/ratings/traveler/4.0-12345-4.gif",
            locationDescription: "Near Bellevue College",
            shortDescription: "<p><b>Location. </b> <br />Located in Bellevue, Larkspur Landing Bellevue - An All-Suite Hotel is in the suburbs and close to Bellevue College, T-Mobile USA Headquarters, and Factoria Mall. Nearby",
            highRate: 64.45,
            lowRate: 51.56,
            rateCurrencyCode: "GBP",
            latitude: 47.57755,
            longitude: -122.13193,
            proximityDistance: 2.0668056,
            proximityUnit: "MI",
            hotelInDestination: true,
            thumbNailUrl: "/hotels/1000000/130000/127100/127040/127040_14_t.jpg",
            deepLink: "http://travel.ian.com/index.jsp?pageName=hotAvail&cid=55505&hotelID=161395&mode=2&numberOfRooms=1&room-0-adult-total=2&room-0-child-total=0&arrivalMonth=4&arrivalDay=17&departureMonth=4&departureDay=19&showInfo=true&locale=en_US&currencyCode=GBP",
            RoomRateDetailsList: {
                RoomRateDetails: {
                    roomTypeCode: 200207592,
                    rateCode: 201183732,
                    maxRoomOccupancy: 2,
                    quotedRoomOccupancy: 2,
                    minGuestAge: 0,
                    roomDescription: "Studio Suite Accessible",
                    propertyAvailable: true,
                    propertyRestricted: false,
                    expediaPropertyId: 127040,
                    RateInfos: {
                        @size: "1",
                        RateInfo: {
                            @rateChange: "false",
                            @promo: "true",
                            @priceBreakdown: "true",
                            RoomGroup: {
                                Room: {
                                    numberOfAdults: 2,
                                    numberOfChildren: 0,
                                    rateKey: "adbdfb1a-df03-4577-9970-ed62f4c84a17"
                                }
                            },
                            ChargeableRateInfo: {
                                @total: "117.14",
                                @surchargeTotal: "14.02",
                                @grossProfitOnline: "18.82",
                                @grossProfitOffline: "11.21",
                                @nightlyRateTotal: "103.12",
                                @maxNightlyRate: "51.56",
                                @currencyCode: "GBP",
                                @commissionableUsdTotal: "158.4",
                                @averageRate: "51.56",
                                @averageBaseRate: "64.45",
                                NightlyRatesPerRoom: {
                                    @size: "2",
                                    NightlyRate: {
                                        0: {
                                            @promo: "true",
                                            @rate: "51.56",
                                            @baseRate: "64.45"
                                        },
                                        1: {
                                            @promo: "true",
                                            @rate: "51.56",
                                            @baseRate: "64.45"
                                        }
                                    }
                                },
                                Surcharges: {
                                    @size: "1",
                                    Surcharge: {
                                        @amount: "14.02",
                                        @type: "TaxAndServiceFee"
                                    }
                                }
                            },
                            nonRefundable: false,
                            rateType: "MerchantStandard",
                            promoId: 202578064,
                            promoDescription: "Sale! Save 20% on this Stay.",
                            promoType: "Standard",
                            currentAllotment: 3
                        }
                    },
                    ValueAdds: {
                        @size: "1",
                        ValueAdd: {
                            @id: "1024",
                            description: "Free High-Speed Internet"
                        }
                    }
                }
            }
        }
    }
}
代码行抛出错误

IList<JToken> rates = root["HotelListResponse"]["HotelList"]["HotelSummary"]["RateInfos"]["RateInfo"].Children().Values().ToList();
IList rates=root[“HotelListResponse”][“HotelList”][“HotelSummary”][“RateInfos”][“RateInfo”]。Children().Values().ToList();
如果有人能告诉我一种eaiser方法,而不是我正在做的深入json数据,我将不胜感激

谢谢
George

此JSON中没有数组。您应该使用
JToken.Parse()
来解析它。然后,您可以使用
SelectToken
提取所需的值:

JToken token = JToken.Parse(json);

int currentAllotment = 
    token.SelectToken("HotelList.HotelSummary.RoomRateDetailsList.RoomRateDetails.RateInfos.RateInfo.currentAllotment")
         .Value<int>();
JToken-token=JToken.Parse(json);
int当前分配=
token.SelectToken(“HotelList.HotelSummary.RoomRateDetails列表.RoomRateDetails.RateInfos.RateInfo.CurrentAllocation”)
.Value();

Fiddle:

似乎它在抱怨RateInfos是一个JArray。你试过IList rates=root[“HotelListResponse”][“HotelList”][“HotelSummary”][“RateInfos”][0][“RateInfo”]。Children().Values().ToList();当然,这只会让你得到第一个费率信息。至于更好的访问数据的方法,我不知道有什么类似于xml的json后代类型支持。Hi cgotberg是的,我读过的书和在线阅读的信息都说json数组被[]包围,但没有一个数据有方括号,所以为什么它抱怨数组索引。我正在认真考虑切换回xml,对我来说,这更容易再次查看JSON。可能问题是您排除了这一层次结构RoomRateDetails列表:{RoomRateDetails:{。您可以尝试IList rates=root[“HotelListResponse”][“HotelList”][“HotelSummary”][“RoomRateDetails列表”][“RoomRateDetails”][“RateInfos”][“RateInfo”].Children().Values().ToList();这是一个非常复杂的对象,理想情况下,您可以将其反序列化为适当的对象。您好,cgotberg,我之前尝试过,错误来自RoomRateDetailsList,这会引发错误:使用无效键值访问JArray值:“RoomRateDetailsList”“.Array position index应为。我不确定处理此问题的最佳方法是什么。您可以通过调用var myTest=someJobject.Children().FirstOrDefault(x=>x.Name==“HotelList”)之类的命令来横穿Json”.Value,但这将是这种深度嵌套的问题中的一个难题。也许您可以使用类似以下内容的Xml解决方案:XmlDocument doc=(XmlDocument)JsonConvert.DeserializeXmlNode(json);