如何在asp.net api requrest中传递JSON数据,在控制器操作中使用哪种类型的参数?
我有以下JSON数据:
{
"0": {
"id": 0,
"type": "camera",
"option": [
{
"optionId": 1,
"optionValue": "",
"answered": "false",
"lastanswerd": "false"
}
]
},
"1": {
"id": 1,
"type": "checkCategory",
"option": [
{
"optionId": 1,
"optionValue": "",
"answered": "false",
"lastanswerd": "false"
},
{
"optionId": 2,
"optionValue": "",
"answered": "false",
"lastanswerd": "false"
}
]
}
}
请建议我,这对我来说是新的。
答案 0 :(得分:0)
如果您将json更改为:
[
{
"id": 0,
"type": "camera",
"option": [
{
"optionId": 1,
"optionValue": "",
"answered": "false",
"lastanswerd": "false"
}
]
},
{
"id": 1,
"type": "checkCategory",
"option": [
{
"optionId": 1,
"optionValue": "",
"answered": "false",
"lastanswerd": "false"
},
{
"optionId": 2,
"optionValue": "",
"answered": "false",
"lastanswerd": "false"
}
]
}
]
public class Option
{
public int optionId { get; set; }
public string optionValue { get; set; }
public string answered { get; set; }
public string lastanswerd { get; set; }
}
public class RootObject
{
public int id { get; set; }
public string type { get; set; }
public List<Option> option { get; set; }
}
答案 1 :(得分:0)
Asp.net MVC自动化并尝试转换每个键:json的值。
{"key1":"value", "key2":"value"}
您可以使用函数中的参数
来访问这些值public ActionResult Index(string key,string key2)
或者创建一个公共setter等于你的键和无参数构造函数的类。
public Class MyJson
{
public string key1 {get;set;}
public string key2 {get;set;}
}
public ActionResult Index(MyJson model)
在您的特定情况下,您传递一个对象列表,因此您应该使用
public ActionResult Index(List<MyJson> model)