Sezer Erdogan Sezer Erdogan - 11 months ago 164
JSON Question

how to take as parameter json list to rest web api service

I have to take json list as parameter for web api.

http://localhost:8082/api/Values/EmptyCardList?number=[
{
num: "1"
},
{
num: "2"
},
{
num: "3"
},
{
num: "4"
},
{
num: "5"
},
{
num: "6"
},
{
num: "7"
}
]


Is it possible ? Can it lead performance problem? Also parameter how much take character or list ?

Answer Source

If you are making your httpPost request and passing json object in your request body

Set

contentType:"application/json"

and in data use JSON.stringify(yourJson);

Something like this:

    $(function () {
    var youJsondata = {num :"2",num:"3"};
    $.ajax({
        type: "POST",
        data :JSON.stringify(youJsondata),
        url: "http://localhost:8082/api/Values/emptycardlist",
        contentType: "application/json"
    });
});

You api method should look something like this:

[HttpPost]
Route("api/Values/emptycardlist")
public HttpResponseMessage EmptyCardList([FromBody] JObject jobject){
   dynamic numList = jobject;
}

Reference

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download