MrM
MrM

Reputation: 65

Newtonsoft Json deserializer not working correctly

I am building an application that handles surveys for people to fill in, in a C# environment.

When I run the application normally, everything works fine, the deserialize works, the serialize works, everything works.

When I send a survey, everything again goes fine. When i re-open the application, the program automatically sends a request to the server to retrieve the surveys available. But when he tries to deserialize the json, he sees he needs to make 2 sub objects but does not fill them.

I checked to see if the json I was getting was correct, and it is. The problem seems that the deserialize doesn't know where the data has to go (even though he normally works with the exact same method).

What I get instead is:

The json looks like this (please keep in mind I kept the data itself out and it is a stripped down version):

{
"id": "data",
"name": "data",
"description": "data",
"surveyVersion": "data",
"welcomeMessage": "data",
"exitMessage": "data",
"anonymousAnswers": false,
"publicationDate": "data",
"expirationDate": "data",
"serverInstanceName": "data",
"documentVersion": "data",
"questions": [
    {
        "id": "data",
        "position": 0,
        "text": "data",
        "answerRequired": true,
        "dependsOn": null,
        "dependencyType": null,
        "dependencyParameter": null,
        "choices": null,
        "type": {
            "name": "data",
            "hasChoices": false,
            "multipleAnswersPossible": false
        },
        "defaultValue": null,
        "openOption": null,
        "maxOpenOption": null,
        "maxCharacters": "data",
        "minimumAnswers": null,
        "maximumAnswers": null,
        "answers": null,
        "openAnswers": null
    }
]}

I checked the json using http://jsonlint.com/ and it is correct, I also checked the data manually and it contains all the data I need.

Now I don't know how I can make sure he deserializes normally if he doesn't want to do this. If I did not send a survey, everything works fine, this only happens after I send a survey and restart the application.

code where the error occurs:

if (jSONSurvey != "") {
            if (newSurveys != null) {
                var root = JsonConvert.DeserializeObject<RootObject> (jSONSurvey);
                if (root.newSurveys != null)
                    newSurveys = root.newSurveys;
            } else {
                newSurveys = new List<NewSurvey> ();
                var root = JsonConvert.DeserializeObject<RootObject> (jSONSurvey);
                if (root.newSurveys != null)
                    newSurveys = root.newSurveys;
            }

The crash part is the else statement.

Upvotes: 1

Views: 5204

Answers (1)

Kami
Kami

Reputation: 19407

The RootObject class structure appears not to match the supplied data.

Try something like this to debug if it's the class or conversion that is failing.

 var root = JsonConvert.DeserializeObject(jSONSurvey);
 Console.WriteLine(root.id);

If the root object is populated with data, then the structure of your RootObject class does not allow for the properties.

Upvotes: 3

Related Questions