有一个标题相同的类似问题,但该解决方案对我的问题无效。
我正在尝试序列化以下 JSON:
{"Id":1,
"Questions":
[{"Id":"q-1-Q0001","Text":"Volume Too High"},
{"Id":"q-1-Q0002","Text":"Volume Too Low"}],
"Text":"My text."}
在我的 C# 中使用此结构:
public class Issue
{
public Issue() { Questions = new List<Question>(); }
public string Id { get; set; }
public List<Question> Questions { get; set; }
public string Text { get; set; }
}
public class Question
{
public string Id { get; set; }
public string Text { get; set; }
}
我让 JavaScript 将带有上述 JSON 的 POST 发送到此 C# 函数:
public JsonResult AddIssueToQueue(Issue issue)
{
var id = issue.Id; // Set correctly
var text = issue.Text; // Set correctly
var q = issue.Questions; // NOT set correctly. Set to List of two empty Question items.
}
id 和 text 设置正确,但 q 设置为包含两个空 Question 对象的 List (Id 和 Text 在每个中为空)。
我的 JSON 格式是否不正确?为什么 Questions 数组不能正确传播?