2014-06-09 96 views
1

我正在使用Web Api和Ember JS,所有工作都很好,直到我開始使用Ember Data,它似乎需要包含在json中的對象名稱。WEB API JSON結果與對象名稱

我該如何添加?

目前越來越:

[ 
    { 
    "id": 1, 
    "title": "maxima", 
    "subTitle": null, 
    "description": "Maxima de boot", 
    "image1": null, 
    "image2": null, 
    "active": false, 
    "skipper": null 
    }, 
    { 
    "id": 2, 
    "title": "beatrix", 
    "subTitle": null, 
    "description": "Beatrix de boot", 
    "image1": null, 
    "image2": null, 
    "active": false, 
    "skipper": null 
    } 
] 

非常感謝!

回答

2

已經使用Web API和Ember Data,你會發現使用序列化器修復json客戶端更容易。假設你正在尋找的職位,`this.store.find(「後」),您將創建一個自定義序列爲它:

App.PostSerializer = DS.RESTSerializer.extend({ 
    extractArray: function(store, type, payload) { 
    payload = {posts: payload}; 
    return this._super(store, type, payload); 
    } 
}); 

例子:http://emberjs.jsbin.com/OxIDiVU/623/edit

瞭解更多關於它(和其他方法)這裏http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_extractArray這裏https://github.com/emberjs/data/blob/master/TRANSITION.md

1

Ember數據不知道如何映射數據,您的json應該看起來像這樣。

{ blogs: [ 
    { "id": 1, "title": "maxima", "subTitle": null, "description": "Maxima de boot", "image1": null, "image2": null, "active": false, "skipper": null }, 
    { "id": 2, "title": "beatrix", "subTitle": null, "description": "Beatrix de boot", "image1": null, "image2": null, "active": false, "skipper": null } ] 
} 

現在,我們知道來自服務器的對象應該映射到博客模型。

+1

嗨馬丁,謝謝你的回覆!是的,我知道,問題是web api默認行爲輸出上面的json沒有對象名稱,我想知道如何將web api中的輸出更改爲正確的格式。 – mausinc

+0

您可以通過以下網址提供的解決方案來解決此問題:http://www.emadibrahim.com/2014/04/09/emberjs-and-asp-net-web-api-and-json-serialization/然而,是另一個問題,因爲此解決方案適用於GET,對於其他VERB,您將在服務器端收到空值。 – codebased

0

我在這個漂亮的新和以前沒有與Ember的工作,但......,而不是返回的「東西」的數組對象的,你能不能修改代碼返回對象內部只包含一個「東西」對象數組?然後,我期望默認的json序列化程序應該爲您處理這個問題。