c# - JSON Data from a WebMethod -


i have following json string has been returned webmethod.

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

i'd present in webpage follows:

 aka  peter pan  donald duck   countries  us, uk   gender  male   percentage  79 

with each set of data being presented in asp listview.

i've come far, i'm struggling values of aka, countries.

            $.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 = "";                 (var = 0; < arrayobj.length; i++) {                     str += arrayobj[i] + "<br/>";                 }                  return str;             } 

* edit *

ok, must of been weekend heat, find other errors morning. revised jquery script below:

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

so produces output required, layout isn't i'd like, not being ui developer , all. how can present output in listview?

*edit *

raw html

display

edit ok layout sorted, reason, 6 entries, when there 3 data sets: results

use $.each on arrays well:

$.each(class["aka"], function(index,ele){     tr += joinwithbr(ele); }); 

ditto 'countries'.

the relevant section of code like:

                    var tr = "<tr><td>";                     $.each(entity["aka"], function (index, ele) {                         tr += ele + "<br>";                     });                     tr+= "</td><td>"                     $.each(entity["countries"], function (index, ele) {                         tr += ele + "<br>";                     });                     tr+= "</td>";                     //continue adding gender, percentage, etc. 

Comments