TyForHelpDude TyForHelpDude - 1 year ago 78
JSON Question

cant get the content of the jsonresult data

client make get request to web api then get response as json data here is tho request and the web api that post response to client data..

This is web api method, after some process post response to client;

public ActionResult GetBooks(){
return new JsonResult()
Data = new { draw = draw, recordsFiltered = totalRecords, recordsTotal = totalRecords, data = booklist },
JsonRequestBehavior = JsonRequestBehavior.AllowGet

and here is the client getting response from web api and post it to view:

HttpResponseMessage response = client.GetAsync("api/Book/GetBooks/").Result;
JsonResult result = null;
if (response.IsSuccessStatusCode)
result = response.Content.ReadAsAsync<JsonResult>().Result;
List<Book> booklist = null;

return Json(new { draw = result.Data.draw, recordsFiltered = result.Data.totalRecords, recordsTotal = result.Data.totalRecords, data = result.Data.booklist }, JsonRequestBehavior.AllowGet);

as you see same properties I try to get them with

but couldn't.

I don't want to change return property names in client method.

What shall I do?

here is response(result.) data look like:

"draw": "1",
"recordsFiltered": 670,
"recordsTotal": 670,
"data": [
"Title": "A Popular Survey of the Old Testament",
"Publisher": "Baker Academic",
"Description": "Illustrated with photos, charts, and maps, and written in their understanding of Old Testament people and events.",
"Authors": [
"Norman L. Geisler"
"Id": "579273aa2711d31de88933bd"

"Title": "A Village on the Moon / Um Povoado Na Lua",
"Publisher": "Trafford Publishing",
"Description": "Since the beginning of time, mankind has looked up at the Moon and wondered. Many have seen figures on that light in the sky and universo. Um grupo de aventureiros se propôs a colonizar esse planeta e aqui está a sua história.",
"Authors": [
"Charles A. Hindley"
"Id": "579273aa2711d31de8893438"

in proper json format we are able to get data with
but in this case I am in trouble..

how can I remove excess scopes ?

Answer Source

Using inner scope the following models can be derived for the client.

public class BookData
    public string Title { get; set; }
    public string Publisher { get; set; }
    public string Description { get; set; }
    public string[] Authors { get; set; }
    public string Id { get; set; }

public class GetBooksResult
    public string draw { get; set; }
    public int recordsFiltered { get; set; }
    public int recordsTotal { get; set; }
    public BookData[] data { get; set; }

With that

HttpResponseMessage response = await client.GetAsync("api/Book/GetBooks/");
GetBooksResult result = null;
if (response.IsSuccessStatusCode)
    result = await response.Content.ReadAsAsync<GetBooksResult>();
return Json(result, JsonRequestBehavior.AllowGet);

On the client the response is raw json string being returned form the web api. No need for JsonResult. You parse the data returned into the models on the client side and pass that on.

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