包含数组的 JSON 上的 JavaScriptSerializer
有一个具有相同标题的类似问题,但解决方案对我的问题无效。
我正在尝试序列化以下 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 和文本设置正确,但 q 设置为包含两个空的列表问题对象(Id 和 Text 均为 null)。
我的 JSON 格式是否不正确?为什么 Questions 数组不能正确传播?
There is a similar question with the same title, but the solution is not valid for my problem.
I'm trying to serialize the following JSON:
{"Id":1,
"Questions":
[{"Id":"q-1-Q0001","Text":"Volume Too High"},
{"Id":"q-1-Q0002","Text":"Volume Too Low"}],
"Text":"My text."}
With this structure in my 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; }
}
I have JavaScript send a POST with the JSON above to this C# function:
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 and text are set correctly, but q is set to a List that contains two empty Question objects (Id and Text are null in each).
Is my JSON formatted incorrectly? Why doesn't the Questions array propagate correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这只是一个大胆的猜测,但您的 JSON 结构有一个带有整数的 ID,如上面 rsbarro 提到的。但是 C# 中的代理类需要一个字符串 - 类型转换是否可能会混淆?
This is a just a wild guess, but your JSON structure has an ID with an integer, as rsbarro mentioned above. But your proxy class in C# is expecting a string - is it possible the type conversion is getting mixed up there?
这是我的 ajax 调用,工作正常,我收到问题列表
你也可以分享你的代码吗?
This my ajax call and it is working fine I am getting the question List
Can you share your code as well.