C# &引用;对象引用未设置为对象的实例;在反序列化类中循环时
编辑:事实证明,我可以很好地反序列化,问题实际上是当我试图通过循环抓住问题时。尽管“对象引用未设置为对象的实例”,但仍存在相同错误。我之所以编辑这篇文章,是因为我现在无法删除这篇有答案的文章C# &引用;对象引用未设置为对象的实例;在反序列化类中循环时,c#,json,json.net,surveymonkey,C#,Json,Json.net,Surveymonkey,编辑:事实证明,我可以很好地反序列化,问题实际上是当我试图通过循环抓住问题时。尽管“对象引用未设置为对象的实例”,但仍存在相同错误。我之所以编辑这篇文章,是因为我现在无法删除这篇有答案的文章 //deserialize json ResponsesList responses = JsonConvert.DeserializeObject<ResponsesList>(_ResponseContent); if
//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);
}
}
这是我要反序列化的类
//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; }
}
}
//获取\u响应
公共类响应列表
{
公共int状态{get;set;}
公共列表数据{get;set;}
公共类数据
{
公共字符串应答器_id{get;set;}
公共列表问题{get;set;}
}
公开课问题
{
公共列表答案{get;set;}
公共字符串问题_id{get;set;}
}
公开课答案
{
公共字符串行{get;set;}
公共字符串col{get;set;}
}
}
我将使用此网站创建您的课程:
这就是它所说的:
public class RootObject
{
public int status { get; set; }
public List<object> data { get; set; }
}
公共类根对象
{
公共int状态{get;set;}
公共列表数据{get;set;}
}
我刚刚成功地在LINQPad中反序列化了您的示例字符串:
var str =
@"{
""status"": 0,
""data"": [
null,
null
]
}";
JsonConvert.DeserializeObject<ResponsesList>(str).Dump();
var str=
@"{
“状态”:0,
“数据”:[
无效的
无效的
]
}";
JsonConvert.DeserializeObject(str.Dump();
这告诉我你的反应内容并不是你想象的那样。明白了。它只需要进行一些检查,以确保我尝试循环通过的对象不是空的 例如:
//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);
}
}
}
}
//反序列化json
ResponseList responses=JsonConvert.DeserializeObject(\u ResponseContent);
如果(响应!=null)
{
//循环响应
foreach(ResponsesList.Data在responses.Data中的数据)
如果(数据!=null)
{
foreach(响应列表。数据中的问题。问题)
如果(问题!=null)
{
foreach(ResponseList.Answer-Answer-Answer-in-question.answers)
{
//插入每个响应
上级回复(调查id、数据.受访者id、问题.问题id、答案.行、答案.列);
}
}
}
}
尽管如此,我还是很欣赏大家的回答。当我遇到这个错误时,我通过在执行循环之前添加一个null测试来修复我的错误。您是否尝试过初始化构造函数中的
数据列表?堆栈跟踪是什么?什么是\u ResponseContent
?它看起来不像是初始化了 //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);
}
}
}
}