A
A
Andrey2019-04-27 21:38:51
JSON
Andrey, 2019-04-27 21:38:51

How to get (deserialize) a single value from a large JSON response?

Good afternoon! Tell me, please, how can I get one value from a large json as simply as possible?
There is 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
            }
        }
    ]
}

I'm trying to get a value from it, where the name of the organization is short. For example: "value": "ООО \"СТЕКЛОКОН\"",
But I'm having a hard time :( Either nothing is deserialized, or an exception...
I'll be glad to hear your advice!
My code:
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);
                }

I'm trying to get inspired by the official documentation, https://www.newtonsoft.com/json/help/html/Deserial... , but without much success.
Thank you!
PS I forgot to add that the application is on Xamarin.Forms

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Pogrebnyak, 2019-04-27
@andrey71

as simple as possible

try {
dynamic json = JsonConvert.DeserializeObject<dynamic>(response);
string myValue = json.suggestions[0].value.ToString();
}
catch (JSONException je){
// handle
}

N
Nikolay, 2017-03-26
@keche

min-height- you set your internal blocks, add indents and all the things, if necessary, setoveflow: hidden

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question