2

有一个标题相同的类似问题,但该解决方案对我的问题无效。

我正在尝试序列化以下 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 数组不能正确传播?

4

2 回答 2

0

这是我的 ajax 调用,它工作正常我收到问题列表

   $.ajax({
                type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: "{'issue':{'Id':1,'Questions':[{'Id':'q-1-Q0001','Text':'Volume Too High'},{'Id':'q-1-Q0002','Text':'Volume Too Low'}],'Text':'My text.'}}" ,

            dataType: 'html',
            url: 'AddIssueToQueue',
            success: function (data) {
                if (data) {
                    //Do something 
                }
            }
        });

你也可以分享你的代码。

于 2011-08-07T05:28:19.377 回答
0

这只是一个疯狂的猜测,但您的 JSON 结构有一个带有整数的 ID,正如上面提到的 rsbarro。但是您在 C# 中的代理类需要一个字符串 - 类型转换是否可能在那里混淆?

于 2011-08-07T04:29:35.077 回答