我有以下從Webmethod返回的JSON字符串。來自WebMethod的JSON數據
[{"__type":"DEV.GlobalClasses+Class","AKA":["Peter Pan","Donald Duck"],"Countries":["US","UK"],"Gender":"Male","PercentageMatch":94},{"__type":"DEV.GlobalClasses+Class,"AKA":["Andrew"],"Countries":["FR"],"Gender":null,"PercentageMatch":72}]
我想目前它在網頁中,如下所示:
AKA
Peter Pan
Donald Duck
Countries
US, UK
Gender
Male
Percentage
79
隨着每一組數據被呈現爲我將在ASP的ListView。
我已經拿出這個到目前爲止,但我努力獲得AKA,國家的價值觀。
$.ajax({
type: "POST",
url: "Default.aspx/PopulatePopUp",
cache: false,
data: JSON.stringify({ messageId: messageId, messageType: messageType }),
// data: '{ messageId:' + messageId + ', messageType:' + messageType + ' }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var classes= msg.d;
$.each(classes, function (index, class) {
var table = $("<table><thead><tr><th>AKA</th><th>Countries</th><th>Gender</th><th>Percentage</th></thead><tbody>");
var tr = "<tr>";
tr += "<td>" + joinWithBr(class["AKA"].string) + "</td>";
tr += "<td>" + joinWithBr(class["Countries"].string) + "</td>";
tr += "<td>" + class["Gender"] + "</td>";
tr += "<td>" + class["PercentageMatch"] + "</td>";
tr += "</tr>";
table.append(tr);
});
table += '</tbody></table>';
$('div#results').html(table);
}
});
function joinWithBr(arrayObj) {
var str = "";
for (var i = 0; i < arrayObj.length; i++) {
str += arrayObj[i] + "<br/>";
}
return str;
}
*編輯*
好,必須的了週末熱,但我今天早上找到一些其他錯誤。她下面的修訂jQuery腳本:
success: function (msg) {
var entities = msg.d;
var table = $("<table><thead><tr><th>AKA</th><th>Countries</th><th>Gender</th><th>Percentage</th></thead><tbody></tbody></table>");
$.each(entities, function (index, entity) {
var tr = "<tr>";
$.each(entity["AKA"], function (index, ele) {
tr += "<td>" + ele + "<br/>" + "</td>";
});
$.each(entity["Countries"], function (index, ele) {
tr += "<td>" + ele + "<br/>" + "</td>";
});
tr += "<td>" + entity["Gender"] + "</td>";
tr += "<td>" + entity["Percentage"] + "</td>";
tr += "</tr>";
table.append(tr);
});
$('div#results').html(table);
所以要求這個現在生產輸出,但佈局並不像我想,不是UI開發人員和所有。我如何在ListView中顯示輸出?
*編輯*
編輯 確定這樣的佈局現在可以正確排序,但出於某種原因,我得到六個條目,當有隻有3組數據:
'class'是在JavaScript保留關鍵字。首先用別的東西改變它。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words – Satpal
歡呼聲Satpal,它只是爲了說明目的在這個專欄。 – CSharpNewBee