如何在 jQuery 中读取 JSON 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17403103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:59:24  来源:igfitidea点击:

How to read JSON array in jQuery

javascriptjqueryasp.net-mvcjson

提问by Maven

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

我正在使用jQuery.load()函数从服务器加载结果并将其附加到我的 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:

在我的控制器中,我尝试将模型中的锯齿状数组转换为 json 对象,然后将其发送到客户端:

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:

在此之后,当我通过console.log(responseText);以下格式检查响应时,我有一个类似 jSon 的字符串:

[
["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:

我不确定这是否是一个合适的 jSon,因为从我学到的 jSon 是名称:值对,但这里只有值,考虑到我如何读取这个字符串/数组并以下列方式打印:

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

回答by iJade

Use var dataArray = jQuery.parseJSON(response);

var dataArray = jQuery.parseJSON(response);

回答by Harsha Venkatram

If your response array is

如果您的响应数组是

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"],
]

you can use

您可以使用

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

Demo: http://jsfiddle.net/KX596/1/

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

回答by Chris Dixon

$.getJSON('Get/Information', { class: "Arts", min: 1 })
.done(function( json ) {
   $.each(json , function(i, v) {
     var id = v[0]; // etc
   });
});

Refer to getJSON

参考getJSON

回答by Arun P Johny

You need to use getJSON()

你需要使用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');
    })
});