【问题标题】:How do I map complex json to model in ASP.Net MVC如何将复杂的 json 映射到 ASP.Net MVC 中的模型
【发布时间】:2014-10-16 10:54:41
【问题描述】:

我已经关注 JSON:

var QuestionTemplate = {
    Name: "Simple addition",
    MathML: "mathML goes here",
    Expression: "a+b",
    QType: "mcq",
    Rules: {
        a: {//a is variable from expression(a+b) 
            variableType: "Single Digit",
            min: "1",
            max: "6"
        },
        b: {//b is variable from expression(a+b)
            variableType: "Two Digit",
            min: "20",
            max: "80"
        }
    }
};

在规则中,“a”和“b”是来自表达式“a+b”的变量。如果表达式为a+b+c,则有a、b、c三个变量。

现在我想将上面的 json 映射到我在 MVC 中的模型。

到目前为止我已经尝试过:

型号:

public class QuestionTemplateModel
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
    public string Name { get; set; }
    public string MathML { get; set; }
    public string Expression { get; set; }
    public string QType { get; set; }
    public Rules Rules { get; set; }

}

public class Rules
{
    public List<variable> variable { get; set; }
}

public class variable
{
    public List<VariableDetails> VariableDetails { get; set; }
}

public class VariableDetails
{
    public string variableType { get; set; }
    public string min { get; set; }
    public string max { get; set; }
}

但是当我将我的 json 发送到模型时,Rules 为 null,其中 Name、MathML、Expression、QType 被正确映射。 我需要如何构建我的模型。我做错了什么。

【问题讨论】:

  • 在您的 JSON 规则属性中是一个集合类型,而在您的 QuestionTemplate 模型中,它是一个对象
  • 您真的很确定在之前就 SO 上的同一主题(模型绑定)给出的众多答案中,没有一个提供解决您的问题所需的信息吗?

标签: c# asp.net-mvc json asp.net-mvc-4 model


【解决方案1】:

我认为您不需要这么多嵌套对象-字典应该可以工作... [编辑]您也需要创建字典。字典键将是您的变量名称(a,b)。试试这个:

public class QuestionTemplateModel
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
    public string Name { get; set; }
    public string MathML { get; set; }
    public string Expression { get; set; }
    public string QType { get; set; }
    public Dictionary<string, VariableDetails> Rules = new Dictionary<string, VariableDetails>() { get; set; }

}



public class VariableDetails
{
    public string variableType { get; set; }
    public string min { get; set; }
    public string max { get; set; }
}

【讨论】:

    【解决方案2】:

    在下面的代码中

    a: {//a is variable from expression(a+b) 
            variableType: "Single Digit",
            min: "1",
            max: "6"
        },
        b: {//b is variable from expression(a+b)
            variableType: "Two Digit",
            min: "20",
            max: "80"
        }
    

    a: 和 b: 不会与任何属性匹配,因此它不会绑定该规则属性, 您可以做的是删除 a: 和 b: 以下列方式命名和格式化您的 JSON

    {
            variableType: "Single Digit",
            min: "1",
            max: "6"
        },
        {
            variableType: "Two Digit",
            min: "20",
            max: "80"
        }
    

    【讨论】:

    • Above ans by @mlinth 将是一个更好的选择,它应该可以工作,如果 JSON 格式在你的控制范围内,那么在我的 ans 中尝试上面的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2014-11-13
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    相关资源
    最近更新 更多