Icet
Icet

Reputation: 688

JSON Deserialize Object

I've got problem with JSON Deserialize Object. I'm getting this json:

{
    "status": "ok",
    "message": "OK",
    "clientData": {
        "id": 1,
        "properties": {
            "imie": "xxx",
            "nazwisko": "xxxx",
            "tel": "5121211",
            "email": "woeee@wwww",
            "pesel": "1111111",
            "kod_pocztowy": "2222",
            "miejscowosc": "eeee",
            "kwota_wnioskowana": 1,
            "dochod": 11,
            "bik": null,
            "firma_nip": "1",
            "komornik": null,
            "oddluzanie": null,
            "leadID": "I1"
        },
        "subcategories": {
            "67": 2001,
            "69": 2003
        }
    },
    "linksData": {
        "3": {
            "linkId": 574,
            "link": "ddd",
            "kartaPryw": 1,
            "produktNazwa": "xxx",
            "alternatives": {
                "114": {
                    "linkId": 114,
                    "link": "xxx",
                    "kartaPryw": 1
                },
                "513": {
                    "linkId": 513,
                    "link": "xxx",
                    "kartaPryw": 1
                },
                "532": {
                    "linkId": 532,
                    "link": "xxx",
                    "kartaPryw": 1
                }
            }
        },
        "12": {
            "linkId": 33,
            "link": "wwww",
            "kartaPryw": 0,
            "produktNazwa": "ee",
            "alternatives": {
                "38": {
                    "linkId": 38,
                    "link": "wwww",
                    "kartaPryw": 0
                },
                "40": {
                    "linkId": 40,
                    "link": "qqqq",
                    "kartaPryw": 0
                },
                "665": {
                    "linkId": 665,
                    "link": "wwww",
                    "kartaPryw": 0
                }
            }
        },
        "14": {
            "linkId": 116,
            "link": "ww",
            "kartaPryw": 0,
            "produktNazwa": "w",
            "alternatives": {
                "123": {
                    "linkId": 123,
                    "link": "ffff",
                    "kartaPryw": 0
                },
                "124": {
                    "linkId": 124,
                    "link": "ffff",
                    "kartaPryw": 0
                },
                "517": {
                    "linkId": 517,
                    "link": "wwww",
                    "kartaPryw": 0
                },
                "519": {
                    "linkId": 519,
                    "link": "ffff",
                    "kartaPryw": 0
                }
            }
        },
        "17": {
            "linkId": 404,
            "link": "ffff",
            "kartaPryw": 0,
            "produktNazwa": "ee",
            "alternatives": []
        },
        "26": {
            "linkId": 135,
            "link": "eeee",
            "kartaPryw": 0,
            "produktNazwa": "eee",
            "alternatives": []
        },
        "27": {
            "linkId": 790,
            "link": "eeee",
            "kartaPryw": 0,
            "produktNazwa": "ee",
            "alternatives": {
                "792": {
                    "linkId": 792,
                    "link": "eeeee",
                    "kartaPryw": 0
                }
            }
        },
        "34": {
            "linkId": 778,
            "link": "eee",
            "kartaPryw": 0,
            "produktNazwa": "eee",
            "alternatives": []
        }
    },
    "currentCartNr": 1
}

Format is awful here so paste this code here to see it better: http://json.parser.online.fr/

and here are my classes

public class Client
{
    public string Status { get; set; }
    public string Message { get; set; }
    public Data clientData { get; set; }
    public Dictionary<string, linksData> linksData;
    public int currentCartNr { get; set; }
}

public class Data
{
    public int id { get; set; }
    public Dictionary<string, string> properties { get; set; }
    public Dictionary<string, int> subcategories { get; set; }
}

public class linksData
{
    public int linkId { get; set; }
    public string link { get; set; }
    public int kartaPryw { get; set; }
    public string produktNazwa { get; set; }
    public Dictionary<string, alternatives> alternatives;
}


public class alternatives
{
    public int linkId { get; set; }
    public string link { get; set; }
    public int kartaPryw { get; set; }
}

and I can't deserialize my JSON, i'm getting error when it try to deserialize "alternatives". If I comment this public Dictionary<string, alternatives> alternatives; it's working but but I need this field. What's wrong with it?

and here it is how I deserialize it:

var myObject = JsonConvert.DeserializeObject<Client>(get_person);

Error message:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,Linker.Class.alternatives]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

UPDATE

Problem solved. alternatives [] was changed to value null and it works.

Upvotes: 2

Views: 554

Answers (2)

Coder1409
Coder1409

Reputation: 498

The problem as I can see is with your serialization not the deserialization. By serializing null values as a dictionary you can't just deserialize it Later the question is where are you getting your JSON file from , is it you who is serializing it? if so all you need to do is change your serialization to this

var jsonText=JsonConvert.serialize(JsonObject, new JsonSerializerSettings() {NullValueHandling= NullValueHandling.Ignore });

deserialization would be the same as you have done in your code

Upvotes: 1

facundofarias
facundofarias

Reputation: 3043

It seems to be an issue on your JSON (I would say). When alternatives contains data, it is okay (it will serialized as a Dictionary) but when it is empty, it shows:

"alternatives": []

Which I think you have to configure your parser otherwise will fail. At least with Jackson for Java this is configurable. Which one are u using? I guess it can be configured as well (Perhaps Json.NET?)

Upvotes: 2

Related Questions