0

EDIT: turns out I can deserialize just fine, the problem is actually when I try to loop through to grab the questions. Same error though "Object reference not set to an instance of an object". I am only editing this because I can't delete the post now that it has answers.

            //deserialize json
            ResponsesList responses = JsonConvert.DeserializeObject<ResponsesList>(_ResponseContent);

            if (responses != null)
            {
                //loop through responses
                foreach (ResponsesList.Data data in responses.data)
                    foreach (ResponsesList.Questions question in data.questions)
                        foreach (ResponsesList.Answer answer in question.answers)
                        {
                            //upsert each response
                            UpsertResponse(survey_id, data.respondent_id, question.question_id, answer.row, answer.col);
                        }
            }

This line is where the error occurs

foreach (ResponsesList.Questions question in data.questions)

Here is the class I am deserializing to

    //get_responses
    public class ResponsesList
    {
        public int status { get; set; }
        public List<Data> data { get; set; }

        public class Data
        {
            public string respondent_id { get; set; }
            public List<Questions> questions { get; set; }
        }

        public class Questions
        {
            public List<Answer> answers { get; set; }
            public string question_id { get; set; }
        }

        public class Answer
        {
            public string row { get; set; }
            public string col { get; set; }
        }

    }
swegs
  • 55
  • 8

4 Answers4

1

I just successfully deserialized your sample string in LINQPad:

var str = 
@"{
    ""status"": 0,
    ""data"": [
        null,
        null
    ]
}";
JsonConvert.DeserializeObject<ResponsesList>(str).Dump();

Which tells me that your _ResponseContent is not what you think it is.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
1

figured it out. it just needed to put some checks in to ensure that the objects I was trying to loop through weren't null.

Example:

            //deserialize json
            ResponsesList responses = JsonConvert.DeserializeObject<ResponsesList>(_ResponseContent);

            if (responses != null)
            {
                //loop through responses
                foreach (ResponsesList.Data data in responses.data)
                    if (data != null)
                    {
                        foreach (ResponsesList.Questions question in data.questions)
                            if (question != null)
                            {
                                foreach (ResponsesList.Answer answer in question.answers)
                                {
                                    //upsert each response
                                    UpsertResponse(survey_id, data.respondent_id, question.question_id, answer.row, answer.col);
                                }
                            }
                    }
            }

I appreciate everyone's responses though.

swegs
  • 55
  • 8
0

When I ran into this error I fixed mine by adding a test for null prior to executing the loop.

Demodave
  • 6,242
  • 6
  • 43
  • 58
-1

I would use this site to create your classes: http://json2csharp.com/

This is what it spit out:

public class RootObject
{
    public int status { get; set; }
    public List<object> data { get; set; }
}
SamFisher83
  • 3,937
  • 9
  • 39
  • 52