在循环反序列化类时,“对象引用未设置为对象的实例”

时间:2013-11-22 16:40:18

标签: c# json json.net surveymonkey

编辑:结果我可以反序列化就好了,问题实际上是当我试图循环来抓住问题时。虽然“对象引用未设置为对象的实例”,但错误相同。我只是编辑这个,因为我现在无法删除帖子,因为它有答案。

            //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);
                        }
            }

此行是发生错误的地方

foreach (ResponsesList.Questions question in data.questions)

这是我要反序列化的课程

    //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; }
        }

    }

4 个答案:

答案 0 :(得分:1)

我刚刚在LINQPad中成功反序列化了您的示例字符串:

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

这告诉我你的_ResponseContent不是你想象的那样。

答案 1 :(得分:1)

想通了。它只需要进行一些检查以确保我试图循环的对象不为null。

示例:

            //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);
                                }
                            }
                    }
            }

我很欣赏大家的回应。

答案 2 :(得分:0)

当我遇到这个错误时,我通过在执行循环之前添加一个null测试来修复我。

答案 3 :(得分:-1)

我会用这个网站创建你的课程: http://json2csharp.com/

这就是吐出来的:

public class RootObject
{
    public int status { get; set; }
    public List<object> data { get; set; }
}