0

编辑:结果我可以很好地反序列化,问题实际上是当我尝试循环获取问题时。尽管“对象引用未设置为对象的实例”,但出现相同的错误。我只是在编辑这个,因为我现在不能删除这个帖子,因为它有答案。

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

4 回答 4

1

弄清楚了。它只需要进行一些检查以确保我试图循环的对象不为空。

例子:

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

不过我很感谢大家的回复。

于 2013-11-22T19:18:09.670 回答
1

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

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

这告诉我您的 _ResponseContent 不是您认为的那样。

于 2013-11-22T16:52:16.717 回答
0

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

于 2014-11-17T18:42:09.740 回答
-1

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

这是它吐出来的:

public class RootObject
{
    public int status { get; set; }
    public List<object> data { get; set; }
}
于 2013-11-22T16:43:50.390 回答