2016-07-24 63 views
1

客户做出get请求的Web API,然后得到响应,JSON数据这里是寿请求和网络的API,发布响应客户端数据的内容..不能得到jsonresult数据

这是网络API方法,经过一些流程后对客户的回应;

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

,这里是从网上API客户端得到响应,并邮寄到视图:

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); 

当你看到相同的属性我尝试用result.Data.propertyName 让他们,但不能。

我不想在客户端方法中更改返回属性名称。

我该怎么办?

这里是响应(结果)的数据是这样的:

{{ 
    "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" 
    } 
    ] 
}} 
在适当的JSON格式,我们能够与 result.Data.drawresult.Data.data[0],但在这种情况下,我遇到麻烦数据

..

我怎样才能删除多余的范围?

+0

你的问题不清楚。不确定你在问什么。什么是你想要删除的额外范围 – Nkosi

+0

你好,检查daha它开始于“{{”双范围..它不正确的权利..我不明白为什么它的发生,我该如何解决它 – TyForHelpDude

+0

好了,现在我明白你是什么在问。 – Nkosi

回答

1

使用内部作用域可以为客户端导出以下模型。

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

随着该

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); 

在客户端的响应返回原始JSON字符串形式的网络API。不需要JsonResult。您解析返回到客户端模型中的数据并将其传递。