@tornado_ru

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

"hotels": [
{
"id": 426321,
"countryId": 1104,
"country": "Турция",
"countryUrl": "turkey",
"regionId": 21301,
"region": "Стамбул",
"regionUrl": "istanbul",
"hotelType": "Special Cat.",
"name": "10 KARAKOY HOTEL",
"comment": "",
"imageFolder": "hotel/6456",
"imageHotel": "1_3670.jpg",
"minPrice": "0",
"showHalfPrice": true,
"minPriceCurrency": "",
"restTypes": [],
"tour3D": false,
"video": "https://www.youtube.com/watch?v=_xQo1rGaHlM&featur...",
"tezRecommend": false,
"tezPriority": false,
"tezOnly": false,
"price": "0"
},
{
"id": 413399,
"countryId": 1104,
"country": "Турция",
"countryUrl": "turkey",
"regionId": 21301,
"region": "Стамбул",
"regionUrl": "istanbul",
"hotelType": "3 *",
"name": "ABEL HOTEL",
"comment": "Отель расположен в историческом центре города. Из террасы отеля открывается вид на Мраморное море и старый город.",
"imageFolder": "hotel/4516",
"imageHotel": "Abel-Hotel-photos-Exterior__5__2069.JPEG",
"minPrice": "0",
"showHalfPrice": true,
"minPriceCurrency": "",
"restTypes": [],
"tour3D": false,
"tezRecommend": false,
"tezPriority": false,
"tezOnly": false,
"price": "0"
},
  • Вопрос задан
  • 186 просмотров
Решения вопроса 1
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
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы