2015-09-02 141 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)