2015-09-02 103 views
0

如何在asp.net api中传递JSON数据与控制器操作中哪些类型的参数一起请求?对ASP.NET API的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

如果您改变了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" 
     } 
    ] 
    } 
] 

http://json2csharp.com/给我们:

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; } 
} 
0

Asp.net MVC自动映射,并尝试施放每个键:JSON的价值。

{"key1":"value", "key2":"value"} 

您可以访问功能

public ActionResult Index(string key,string key2) 

或者创建拥有公共二传手等于你的密钥和参数的构造函数类内部使用PARAMS这些值。

public Class MyJson 
{ 
    public string key1 {get;set;} 
    public string key2 {get;set;} 
} 

public ActionResult Index(MyJson model) 

在特定情况下,你传递对象的列表,你应该使用

public ActionResult Index(List<MyJson> model)