@andrey71
админю и учу C#, ASP.NET Core

Как из большого ответа JSON получить (десериализовать) одно значение?

Добрый день! Подскажите, пожалуйста, как можно максимально просто получить одно значение из большого json?
Есть json:
JSON
{
    "suggestions": [
        {
            "value": "ООО \"СТЕКЛОКОН\"",
            "unrestricted_value": "ООО \"СТЕКЛОКОН\"",
            "data": {
                "kpp": "772501001",
                "capital": null,
                "management": {
                    "name": "Овсепян Хачатур Меликович",
                    "post": "Директор"
                },
                "branch_type": "MAIN",
                "branch_count": 0,
                "source": null,
                "qc": null,
                "hid": "85db648b39e183fc974a9c5711475cf4ace02101bb28695fb7b55d7c25c5c6eb",
                "type": "LEGAL",
                "state": {
                    "status": "ACTIVE",
                    "actuality_date": 1488326400000,
                    "registration_date": 728179200000,
                    "liquidation_date": null
                },
                "opf": {
                    "type": null,
                    "code": "12300",
                    "full": "Общество с ограниченной ответственностью",
                    "short": "ООО"
                },
                "name": {
                    "full_with_opf": "ОБЩЕСТВО С ОГРАНИЧЕННОЙ ОТВЕТСТВЕННОСТЬЮ \"СТЕКЛОКОН\"",
                    "short_with_opf": "ООО \"СТЕКЛОКОН\"",
                    "latin": null,
                    "full": "СТЕКЛОКОН",
                    "short": "СТЕКЛОКОН"
                },
                "inn": "7725002343",
                "ogrn": "1027739468877",
                "okpo": null,
                "okved": null,
                "okveds": null,
                "authorities": null,
                "documents": null,
                "licenses": null,
                "address": {
                    "value": "г Москва, Даниловская наб, д 2 к 4",
                    "unrestricted_value": "г Москва, Даниловский р-н, Даниловская наб, д 2 к 4",
                    "data": {
                        "postal_code": "115114",
                        "country": "Россия",
                        "region_fias_id": "0c5b2444-70a0-4932-980c-b4dc0d3f02b5",
                        "region_kladr_id": "7700000000000",
                        "region_with_type": "г Москва",
                        "region_type": "г",
                        "region_type_full": "город",
                        "region": "Москва",
                        "area_fias_id": null,
                        "area_kladr_id": null,
                        "area_with_type": null,
                        "area_type": null,
                        "area_type_full": null,
                        "area": null,
                        "city_fias_id": "0c5b2444-70a0-4932-980c-b4dc0d3f02b5",
                        "city_kladr_id": "7700000000000",
                        "city_with_type": "г Москва",
                        "city_type": "г",
                        "city_type_full": "город",
                        "city": "Москва",
                        "city_area": "Южный",
                        "city_district_fias_id": null,
                        "city_district_kladr_id": null,
                        "city_district_with_type": "Даниловский р-н",
                        "city_district_type": "р-н",
                        "city_district_type_full": "район",
                        "city_district": "Даниловский",
                        "settlement_fias_id": null,
                        "settlement_kladr_id": null,
                        "settlement_with_type": null,
                        "settlement_type": null,
                        "settlement_type_full": null,
                        "settlement": null,
                        "street_fias_id": "fb4d2fdd-0b4a-4e8d-b8cd-e7a28cfd5798",
                        "street_kladr_id": "77000000000115400",
                        "street_with_type": "Даниловская наб",
                        "street_type": "наб",
                        "street_type_full": "набережная",
                        "street": "Даниловская",
                        "house_fias_id": "cb4d5826-4621-4eca-b60b-5957eaf6c9aa",
                        "house_kladr_id": "7700000000011540004",
                        "house_type": "д",
                        "house_type_full": "дом",
                        "house": "2",
                        "block_type": "к",
                        "block_type_full": "корпус",
                        "block": "4",
                        "flat_type": null,
                        "flat_type_full": null,
                        "flat": null,
                        "flat_area": null,
                        "square_meter_price": "177863",
                        "flat_price": null,
                        "postal_box": null,
                        "fias_id": "cb4d5826-4621-4eca-b60b-5957eaf6c9aa",
                        "fias_level": "8",
                        "kladr_id": "7700000000011540004",
                        "capital_marker": "0",
                        "okato": "45296559000",
                        "oktmo": "45914000",
                        "tax_office": "7725",
                        "tax_office_legal": null,
                        "timezone": "UTC+3",
                        "geo_lat": "55.7083638",
                        "geo_lon": "37.6421572",
                        "beltway_hit": "IN_MKAD",
                        "beltway_distance": null,
                        "qc_geo": "0",
                        "qc_complete": "5",
                        "qc_house": "2",
                        "history_values": null,
                        "unparsed_parts": null,
                        "source": "114115 ГОРОД МОСКВА, НАБЕРЕЖНАЯ ДАНИЛОВСКАЯ, дом 2 корп 4",
                        "qc": "0"
                    }
                },
                "phones": null,
                "emails": null,
                "ogrn_date": 1035763200000,
                "okved_type": null
            }
        }
    ]
}


Пытаюсь получить из него значение, где название организации - короткое. Например: "value": "ООО \"СТЕКЛОКОН\"",
Но терплю не удачи :( Либо ничего не десериализуется, либо исключение...
Буду рад, Вашему совету!

Мой код:
public partial class Opf
    {
        [JsonProperty("type")]
        public object Type { get; set; }

        [JsonProperty("code")]
        public long Code { get; set; }

        [JsonProperty("full")]
        public string Full { get; set; }

        [JsonProperty("short")]
        public string Short { get; set; }
    }
....
....
....
 HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                //Вижу, что ответ приходит
                System.Diagnostics.Debug.WriteLine(reader.ReadToEnd());
                var answer = new JsonSerializer();
                using (var jsonTextReader = new JsonTextReader(reader))
                {  
                    // Тут исключение :(      System.NullReferenceException: Object reference not set to an instance of an object.
                    //https://www.newtonsoft.com/json/help/html/DeserializeObject.htm        
                    Opf opf = answer.Deserialize<Opf>(jsonTextReader);
                    System.Diagnostics.Debug.WriteLine(opf.Short);
                }

Пытаюсь вдохновиться официальной документацией, https://www.newtonsoft.com/json/help/html/Deserial..., но без особого успеха.

Спасибо!
P.S. Забыл добавить, что приложение на Xamarin.Forms
  • Вопрос задан
  • 255 просмотров
Решения вопроса 1
@S0HardCore
максимально просто

try {
dynamic json = JsonConvert.DeserializeObject<dynamic>(response);
string myValue = json.suggestions[0].value.ToString();
}
catch (JSONException je){
// handle
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

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

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