你好,這是我第一次使用MustacheJS用JSON web服務在.NET鬍子JS模板使用JSON收集
目前,我掙扎,我似乎無法找到我在做什麼錯誤的設置這個簡單的例子嘗試:
我的web服務returing以下字符串:
[
{
"ShortDescription":"BOX",
"Description":"BOXING",
"Id":1
},
{
"ShortDescription":"EPL",
"Description":"ENGLISH PREMIER LEAGUE",
"Id":2
}
]
我已經驗證了它的語法與本網站:http://json.parser.online.fr/
,這裏是我使用的HTML代碼:
google.load("jquery", "1");
google.setOnLoadCallback(function() {
$(document).ready(
function() {
$.ajax({
url: "../data.asmx/geteagues",
type: "POST",
dataType: "json",
data: "",
contentType: "application/json; charset=utf-8",
success: function (data) {
var template = "<h1>{{ShortDescription}} {{Description}}</h1>";
var html = Mustache.render(template, data);
$('#sampleArea').html(html);
alert(html);
}
})
}
)
});
我張貼整個JS代碼,因爲我不是很好用JavaScript,基本上我要永遠加載從谷歌最新的JQuery的版本,然後工作我的WS電話。
問題到目前爲止是,當我把一個斷點以下行:
var html = Mustache.render(template, data);
我看到模板SETN ok了,所以做的數據,相同的值,我張貼以上,但.render函數返回:
我似乎它不喜歡的數據。到目前爲止,我已經看到了前來爲以下結構的內聯數據的所有例子:
{
"repo": [
{ "name": "resque" },
{ "name": "hub" },
{ "name": "rip" },
]
}
然後模板這樣定義:
{{#repo}}
<b>{{name}}</b>
{{/repo}}
但是,對唯一的區別我的數據是,我沒有像「回購」的「父母」
在服務器端,我使用名爲JSON.net的.net庫和文檔中的集合是如何erialized:
james.newtonking.com/projects/json/help/html/SerializingCollections.htm
最終的結果不包括父節點的名字,這是我的東西從我的鬍子模板定義丟失:
[
{
"Name": "Product 1",
"ExpiryDate": "2000-12-29T00:00Z",
"Price": 99.95,
"Sizes": null
},
{
"Name": "Product 2",
"ExpiryDate": "2009-07-31T00:00Z",
"Price": 12.50,
"Sizes": null
}
]
這是我失蹤?從我的數據「回購」父節點,所以我可以迭代我的模板?
提前感謝您的時間。
-ed
感謝回答@gonchuki,到目前爲止,我看到的唯一問題是JSON.net序列化我的對象的方式,因爲它可以將它包裝在父節點周圍。 – user1577704 2012-08-06 17:28:07
在一般情況下,您可以在通過JSON序列化程序獲取數組之前將數組封裝到匿名對象中,以便在反序列化時只有一個包含要使用的數組的對象(包含'repo')你的模板。 – gonchuki 2012-08-07 12:44:22