1

I am using jQuery .load() function to load the results from server and append it in my html:

$("#content").load('Get/Information?class=Arts&min=1', 
function (responseText, textStatus, XMLHttpRequest) {
    console.log(responseText);
    // parseJSON(responseText);
});

In my controller I tried converting my jagged array from the model into a json object and then send it to the client:

public JsonResult GetInformation(string class, int min){
    var stdObj = new Student();
    string[][] information = stdObj.GetInformation(class, min);
    return Json(information, JsonRequestBehavior.AllowGet);
}

And after this when I check the response through console.log(responseText); I have a jSon like string in the following format:

[
["93","Title-1","http://stackoverflow.com"],
["92"," Title-2","http://stackoverflow.com"],
["90"," Title-3","http://stackoverflow.com"],
["89"," Title-4","http://stackoverflow.com"],
["89"," Title-5","http://stackoverflow.com"],
null,null,null,null,null]

I am not sure if this a proper jSon, as from what I learn jSon is in name: value pairs, but here there are only values, considering this how can I read this string/array and print in following manner:

This just a representation(pseudo code) of how I required it to be handled:

for(int 1=0; i<response.length();i++){
    $("#content").html('<h1>'+i.Id+'</h1>'+'<p>'+i.Title+'</p>'+'<span>'+i.url+'</span>')
                 .slideDown('slow');
    // $("#content").html('<h1>39</h1>'+'<p>Title-1</p>'+'<span>http://stackoverflow.com</span>');
}
4

4 回答 4

12

利用var dataArray = jQuery.parseJSON(response);

于 2013-07-01T11:14:23.473 回答
2

如果您的响应数组是

myArray = 
[
    ["93","Title-1","http://stackoverflow.com"],
    ["92"," Title-2","http://stackoverflow.com"],
    ["90"," Title-3","http://stackoverflow.com"],
    ["89"," Title-4","http://stackoverflow.com"],
    ["89"," Title-5","http://stackoverflow.com"],
]

您可以使用

for(int i=0; i<myArray.length();i++)
{
    $("#content").html('<h1>'+myArray[i][0]+'</h1>'+'<p>'+myArray[i][1]+'</p>'+'<span>'+myArray[i][2]+'</span>').slideDown('slow');
}

演示:http: //jsfiddle.net/KX596/1/

于 2013-07-01T11:16:51.193 回答
1

您需要使用getJSON()

$.getJSON('Get/Information?class=Arts&min=1', function (response) {
    $.each(response, function(idx, rec){
        $("#content").append('<h1>'+rec.Id+'</h1>'+'<p>'rec.Title+'</p>'+'<span>'+rec.url+'</span>').slideDown('slow');
    })
});
于 2013-07-01T11:14:04.377 回答
1
$.getJSON('Get/Information', { class: "Arts", min: 1 })
.done(function( json ) {
   $.each(json , function(i, v) {
     var id = v[0]; // etc
   });
});

参考getJSON

于 2013-07-01T11:15:15.310 回答