Как получить все пары ключ значение в качестве переменных из словаря
var data = JsonConvert.DeserializeObject<Root>(jsonResponse).Data; foreach (var dict in data) { // here i need to access all key values as a variable ex:var id= 3780b945-d24c-4742-bd11-71c90a0728cb; var reference =14660; var LicenseNumber = 2015013464; }
в данных я получаю ниже JsonData:
{ "Data": [ { "Id": "3780b945-d24c-4742-bd11-71c90a0728cb", "Reference": 14660, "LicenseNumber": "2015013464", "Name": "Alegre Belle", "Headline": "Modern masterpiece with vast terracing and designer finishes.", "Summary": "", "Description": "Alegre Belle’s impressive modern style and beautiful interior spaces are a del", "Benefits": null, "OtherInfo": null, "PropertyType": "Villa", "MinimumStay": 3, "Active": true, "Sleeps": 10, "Bedrooms": 5, "Bathrooms": 5, "Kitchens": 1, "DiningCapacity": 10, "LivingAreaSize": 700, "Continent": "Europe", "Country": "Spain", "StateProvince": "Balearic Islands", "Region": "Ibiza", "SubRegion": "San Jose", "AddressLine2": null, "Longitude": 1.292036, "Latitude": 38.881278, "Housekeeping": "Included", "OtherHousekeepingInfo": "4 hours daily cleaning. Concierge service and chef.", "ChildrenAllowed": "Yes", "SmokingAllowed": "No", "PetsAllowed": "No", "EventsAllowed": "N/A", "WheelchairAccessible": "No", "SecurityDeposit": 5000.00, "LastUpdatedAt": "2019-09-25T01:05:35.283", "ImageUrls": [ "http://res.cloudinary.com/privadia/image/upload/v1562146932/alegre_belle_hq_6.jpg", "http://res.cloudinary.com/privadia/image/upload/v1513933158/lhscukldl22h5d9ag8or.jpg", oudinary.com/privadia/image/upload/v1513933207/lteai46ba6svnef8ao3v.jpg", "http://res.cloudinary.com/privadia/image/upload/v1513933138/jufs9ablepifpkgbzpnr.jpg" ], "Rooms": [ { "Type": "Bedroom", "Name": "Bedroom 1", "Description": "Master Bedroom, double bathroom, fitted dressing room, private terrace and outdoor sitting area. This bedroom takes up the whole top floor." }, { "Type": "Bedroom", "Name": "Bedroom 2 ", "Description": "Large double bedroom with en-suite bathroom." }, { "Type": "Kitchen", "Name": "Kitchen 1", "Description": "Designer, fully equipped Boffi kitchen" } ], "MetaData": [ { "Type": "Suitability", "SubType": "Trip Type", "Name": "Families" }, { "Type": "Suitability", "SubType": "Trip Type", "Name": "Groups" }, { "Type": "Setting & View", "SubType": "Setting & View", "Name": "Gated Community" }, { "Type": "Features", "SubType": "Amenities", "Name": "Wifi" }, { "Type": "Features", "SubType": "Amenities", "Name": "Air Conditioning" }, { "Type": "Features", "SubType": "Outdoor Features", "Name": "Private Gates" } ], "Rates": [ { "From": "2019-06-30T16:00:00", "To": "2019-07-29T10:00:00", "Rate": 22534.219100, "LengthOfStay": 7 }, { "From": "2017-04-29T00:00:00", "To": "2017-05-26T00:00:00", "Rate": 12578.763100, "LengthOfStay": 7 }, { "From": "2020-04-25T16:00:00", "To": "2020-05-22T10:00:00", "Rate": 11956.547100, "LengthOfStay": 7 } ], "CalUrl": "http://api.privadia.com/api/Calendar/ICal/14660", "Unavailable": [ { "From": "2016-07-09T16:00:00", "To": "2016-07-16T10:00:00" }, { "From": "2016-07-23T16:00:00", "To": "2016-08-06T10:00:00" }, { "From": "2019-10-26T16:00:00", "To": "2019-11-02T10:00:00" } ] } ], "Page": 1, "TotalCount": 1, "TrackingId": null }
Что я уже пробовал:
Получить пары ключ-значение в качестве переменных значений ключ должен стать именем переменной, а значение-соответствующим значением для ключа.
var data = JsonConvert.DeserializeObject<Root>(jsonResponse).Data; foreach (var dict in data) { // here i need to access all key values as a variable ex:var id= 3780b945-d24c-4742-bd11-71c90a0728cb; var reference =14660; var LicenseNumber = 2015013464; }
class Root { public List<Dictionary<string, object>> Data { get; set; } }
F-ES Sitecore
Это невозможно сказать, не зная ни одного из типов, о которых идет речь. Что такое корень?
ArfatM
корневой класс
{
общественная список<словарь на<строка, объект=""&ГТ;&ГТ; сведения { получить; набор; }
}
F-ES Sitecore
идентификатор объекта;
foreach (var dict in data)
{
если (дикт.ContainsKey("id"))
{
ИД = дикт["идентификатор"];
}
}