Home > Mobile >  How can I deserialize JSON with C#?
How can I deserialize JSON with C#?

Time:12-13

I have the following JSON string:

string jsonData = @"{""type"":""address"",""found objects"":9,""returned objects"":9,""results"":{""1"":{""city"":""Ceg\u0142\u00f3w"",""citypart"":null,""street"":""Adama Asnyka"",""number"":""11"",""teryt"":""141204"",""simc"":""0668956"",""ulic"":""00470"",""code"":""05 - 319"",""jednostka"":""{ Polska,mazowieckie,mi\u0144ski,Ceg\u0142\u00f3w}
            "",""x"":""688066.62"",""y"":""479206.44"",""geometry_wkt"":""POINT(688066.62 479206.44)"",""id"":1},""2"":{""city"":""Ceg\u0142\u00f3w"",""citypart"":null,""street"":""Adama Asnyka"",""number"":""13A"",""teryt"":""141204"",""simc"":""0668956"",""ulic"":""00470"",""code"":""05 - 319"",""jednostka"":""{ Polska,mazowieckie,mi\u0144ski,Ceg\u0142\u00f3w}
            "",""x"":""688081.88"",""y"":""479250.24"",""geometry_wkt"":""POINT(688081.88 479250.24)"",""id"":2},""3"":{""city"":""Ceg\u0142\u00f3w"",""citypart"":null,""street"":""Adama Asnyka"",""number"":""15"",""teryt"":""141204"",""simc"":""0668956"",""ulic"":""00470"",""code"":""05 - 319"",""jednostka"":""{ Polska,mazowieckie,mi\u0144ski,Ceg\u0142\u00f3w}
            "",""x"":""688126.5"",""y"":""479229.34"",""geometry_wkt"":""POINT(688126.5 479229.34)"",""id"":3},""4"":{""city"":""Ceg\u0142\u00f3w"",""citypart"":null,""street"":""Adama Asnyka"",""number"":""18"",""teryt"":""141204"",""simc"":""0668956"",""ulic"":""00470"",""code"":""05 - 319"",""jednostka"":""{ Polska,mazowieckie,mi\u0144ski,Ceg\u0142\u00f3w}
            "",""x"":""688179.59"",""y"":""479255.470000001"",""geometry_wkt"":""POINT(688179.59 479255.470000001)"",""id"":4},""5"":{""city"":""Ceg\u0142\u00f3w"",""citypart"":null,""street"":""Adama Asnyka"",""number"":""4"",""teryt"":""141204"",""simc"":""0668956"",""ulic"":""00470"",""code"":""05 - 319"",""jednostka"":""{ Polska,mazowieckie,mi\u0144ski,Ceg\u0142\u00f3w}
            "",""x"":""688021.03"",""y"":""479167.77"",""geometry_wkt"":""POINT(688021.03 479167.77)"",""id"":5},""6"":{""city"":""Ceg\u0142\u00f3w"",""citypart"":null,""street"":""Adama Asnyka"",""number"":""6"",""teryt"":""141204"",""simc"":""0668956"",""ulic"":""00470"",""code"":""05 - 319"",""jednostka"":""{ Polska,mazowieckie,mi\u0144ski,Ceg\u0142\u00f3w}
            "",""x"":""688073.16"",""y"":""479175.42"",""geometry_wkt"":""POINT(688073.16 479175.42)"",""id"":6},""7"":{""city"":""Ceg\u0142\u00f3w"",""citypart"":null,""street"":""Adama Asnyka"",""number"":""6A"",""teryt"":""141204"",""simc"":""0668956"",""ulic"":""00470"",""code"":""05 - 319"",""jednostka"":""{ Polska,mazowieckie,mi\u0144ski,Ceg\u0142\u00f3w}
            "",""x"":""688063.45"",""y"":""479133.4"",""geometry_wkt"":""POINT(688063.45 479133.4)"",""id"":7},""8"":{""city"":""Ceg\u0142\u00f3w"",""citypart"":null,""street"":""Adama Asnyka"",""number"":""6B"",""teryt"":""141204"",""simc"":""0668956"",""ulic"":""00470"",""code"":""05 - 319"",""jednostka"":""{ Polska,mazowieckie,mi\u0144ski,Ceg\u0142\u00f3w}
            "",""x"":""688072.54"",""y"":""479095.720000001"",""geometry_wkt"":""POINT(688072.54 479095.720000001)"",""id"":8},""9"":{""city"":""Ceg\u0142\u00f3w"",""citypart"":null,""street"":""Adama Asnyka"",""number"":""8B"",""teryt"":""141204"",""simc"":""0668956"",""ulic"":""00470"",""code"":""05 - 319"",""jednostka"":""{ Polska,mazowieckie,mi\u0144ski,Ceg\u0142\u00f3w}
            "",""x"":""688110.78"",""y"":""479085.880000001"",""geometry_wkt"":""POINT(688110.78 479085.880000001)"",""id"":9}},""request time"":0.027953267097473145}";

and code:

jsonAddresses _jsonAddresses = JsonConvert.DeserializeObject<jsonAddresses>(jsonData);
[System.Serializable]
    public class jsonAddresses
    {
        [JsonProperty(PropertyName = "type")]
        public string type { get; set; }
        [JsonProperty(PropertyName = "found objects")]
        public string foundobjects { get; set; }
        [JsonProperty(PropertyName = "returned objects")]
        public string returnedobjects { get; set; }
        [JsonProperty(PropertyName = "results")]
        public IList<jsonResults> results { get; set; }
    }

    [System.Serializable]
    public class jsonResults
    {
        [JsonProperty(PropertyName = "city")]
        public string city { get; set; }
        [JsonProperty(PropertyName = "citypart")]
        public string citypart { get; set; }
        [JsonProperty(PropertyName = "street")]
        public string street { get; set; }
        [JsonProperty(PropertyName = "number")]
        public string number { get; set; }
        [JsonProperty(PropertyName = "teryt")]
        public string teryt { get; set; }
        [JsonProperty(PropertyName = "simc")]
        public string simc { get; set; }
        [JsonProperty(PropertyName = "ulic")]
        public string ulic { get; set; }
        [JsonProperty(PropertyName = "code")]
        public string code { get; set; }
        [JsonProperty(PropertyName = "jednostka")]
        public string jednostka { get; set; }
        [JsonProperty(PropertyName = "x")]
        public string x { get; set; }
        [JsonProperty(PropertyName = "y")]
        public string y { get; set; }
        [JsonProperty(PropertyName = "geometry_wkt")]
        public string geometry_wkt { get; set; }
        [JsonProperty(PropertyName = "id")]
        public string id { get; set; }
    }

I have problem with deserialize object with list of objects. I think the problem is in the wrong class of the object being in the list, but I don't know which one.

ERROR: Newtonsoft.Json.JsonSerializationException: „Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IList`1[DeserializeJSON.jsonResults]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'results.1', line 1, position 72.”

How should I properly deserialize it?

CodePudding user response:

you have to fix the jsonAddresses class, change a list to a dictionary, for example

     [JsonProperty(PropertyName = "results")]
    public Dictionary<string,jsonResults> results { get; set; }

or if you want the list

public class jsonAddresses
{
    [JsonProperty(PropertyName = "type")]
    public string type { get; set; }
    [JsonProperty(PropertyName = "found objects")]
    public string foundobjects { get; set; }
    [JsonProperty(PropertyName = "returned objects")]
    public string returnedobjects { get; set; }

    public IList<jsonResults> results { get; set; }
    
    [JsonConstructor]
    public  jsonAddresses (JObject results)
    {
        this.results= results.Properties().Select(x => x.Value.ToObject<jsonResults>() ).ToList();
    }
}

CodePudding user response:

It helps to view the JSON string in a nicer or "pretty" view:

string jsonData = @"{
    "type":"address",
    "found objects":9,
    "returned objects":9,
    "results":{
        "1":{
            "city":"Ceg\u0142\u00f3w",
            "citypart":null,
            "street":"Adama Asnyka",
            "number":"11",
            "teryt":"141204",
            "simc":"0668956",
            "ulic":"00470",
            "code":"05 - 319",
            "jednostka":"{ Polska,mazowieckie,mi\u0144ski,Ceg\u0142\u00f3w}",
            "x":"688066.62",
            "y":"479206.44",
            "geometry_wkt":"POINT(688066.62 479206.44)",
            "id":1
        },
        "2":{
            "city":"Ceg\u0142\u00f3w",
            "citypart":null,
            ...
        }
    }
};

Now you can see that the "results" element is not an array. If it were an array, it would be wrapped in []. Instead, "results" is just a JSON object with key value pairs. Each key value pair is "number":{another JSON object}. Either change your JSON structure, or as @Serge suggests, change your C# a bit to properly handle the key value pairs.

  • Related