Json字符串的c#等价类

本文关键字:字符串 Json | 更新日期: 2023-09-27 18:05:53

我有Json字符串,我已经在jquery中创建。现在我要把它发布到服务器端。我无法在c#中创建它的等效类。

这是我的json字符串
[
    {
        "Option": "Sub Option 0",
        "Value": "Question 0",
        "questions": [
            {
                "Option": "Sub Option 1",
                "Value": "Sub Question for 0",
                "questions": [
                    {
                        "Option": "Sub Option 2",
                        "Value": "Sub Question for 1"
                    },
                    {
                        "Option": "Sub Option 5",
                        "Value": "Sub Question for 1",
                        "questions": [
                            {
                                "Option": "Sub Option 6",
                                "Value": "Sub Question for 5",
                                "questions": [
                                    {
                                        "Option": "Sub Option 7",
                                        "Value": "Sub Question for 6"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "Option": "Sub Option 3",
                "Value": "Sub Question for 0"
            },
            {
                "Option": "Sub Option 4",
                "Value": "Sub Question for 0"
            }
        ]
    }
]

我已经试过了。

public class AnyName
{
    public string Option { get; set; }
    public string Value { get; set; }
    public List<AnyName> questions { get; set; }
}

我的动作方法,它将接受数据

[HttpPost]
public void GetQuestion(AnyName allQuestions)
{
    //read all the question here.
    var x = 10;
}

Js:

var myJson = CreateJsonData(childrenQuestions);
            $.ajax({
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                url: '/Home/GetQuestion',
                dataType: "json",
                data: JSON.stringify(myJson),
                success: function (result) {
                }
            });

我在服务器端得到null。allQuestions is null

Json字符串的c#等价类

您可以使用任何名称

public class AnyName
{
    public string Option { get; set; }
    public string Value { get; set; }
    public List<AnyName> Questions { get; set; }
}

要反序列化的类型是List<AnyName>,例如

var list = JsonConvert.DeserializeObject<List<AnyName>>(json);

根据序列化器的不同,您可以设置不同的属性名称。例如,使用Json。. NET中,你可以这样写你的属性:

[JsonProperty("Questions")]
public List<Questions> SubQuestions { get; set; }

见http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonPropertyAttribute.htm

修复了questions属性的大小写,因为它在JSON字符串中是小写的。根据所使用的JSON序列化器,您也可以尝试使用属性来映射它,这可能是特定于序列化器的,但将类属性匹配到JSON中预期的属性名称会更防愚弄。

public class Question
{
    public string Option { get; set; }
    public string Value { get; set; }
    public List<Question> questions { get; set; }
}

List将生成一个Question类的JSON数组,并将从包含适合Question类的结构的JSON数组中正确反序列化。

由于JSON结构有一个JSON问题数组的根,因此您还需要进行以下更改以接受该数组:

[HttpPost]
public void GetQuestion(List<Question> questions)
{
   //read all the question here.
   var x = 10;
}

,丢弃Root类。您不需要基于JSON结构。