• Как вытащить все значения некоторого поля из json на C#?

    vabka
    @vabka Куратор тега C#
    Токсичный шарпист
    Десериализуешь json, потом делаешь data.Select(x=>x.Name).ToArray()
    // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
        public class Hotel    {
            public int id { get; set; } 
            public int countryId { get; set; } 
            public string country { get; set; } 
            public string countryUrl { get; set; } 
            public int regionId { get; set; } 
            public string region { get; set; } 
            public string regionUrl { get; set; } 
            public string hotelType { get; set; } 
            public string name { get; set; } 
            public string comment { get; set; } 
            public string imageFolder { get; set; } 
            public string imageHotel { get; set; } 
            public string minPrice { get; set; } 
            public bool showHalfPrice { get; set; } 
            public string minPriceCurrency { get; set; } 
            public List<object> restTypes { get; set; } 
            public bool tour3D { get; set; } 
            public string video { get; set; } 
            public bool tezRecommend { get; set; } 
            public bool tezPriority { get; set; } 
            public bool tezOnly { get; set; } 
            public string price { get; set; } 
        }
    
        public class Root    {
            public Hotel[] hotels { get; set; } 
        }


    var data = JsonConvert.DeserializeObject<Root>(jsonText);
    var names = data.hotels.Select(x=>x.name).ToArray();

    Используйте Newtonsoft.Json или System.Text.Json
    Ответ написан
    1 комментарий