2016-07-24 2 views
1

クライアントのコンテンツを取得カント、クライアントにいくつかの処理後に応答した後。これは、Web APIメソッドである</p> <p>..ここでJSONデータはカントー要求とクライアントデータへの応答をポストウェブAPIであるとして、応答を取得したWeb APIへのリクエストをプレゼントするJsonResultデータ

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

、ここでのWeb 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 でそれらを取得しようとしたができなかった同じプロパティを参照してくださいよう。

私はクライアントメソッドでリターンプロパティ名を変更したくありません。

どうすればよいですか?ここ

は、応答(結果)データがどのように見えるされています。適切なJSON形式で

{{ 
    "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" 
    } 
    ] 
}} 

我々はresult.Data.drawresult.Data.data[0]でデータを取得することができますが、この場合には、私は困っています。..

余分なスコープを削除するにはどうすればよいですか?

+0

あなたの質問は不明です。あなたが何を求めているのかは不明です。あなたが削除したい余分な範囲は何ですか – Nkosi

+0

こんにちは、 "{{"二重スコープ..その正しい右.. ..私はそれが起こる理由を理解していないと私はそれを修正することができますどのように – TyForHelpDude

+0

あなたが尋ねている。 – Nkosi

答えて

1

内部スコープを使用すると、次のモデルをクライアント用に取得できます。クライアントでその

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

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

応答は、Web APIをフォーム返される生のJSON文字列です。 JsonResultの必要はありません。クライアント側でモデルに返されたデータを解析し、それを渡します。

関連する問題