使用 jQuery / AJAX 解码 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11018527/
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 10:05:44  来源:igfitidea点击:

Decode JSON with jQuery / AJAX

jqueryajaxjsondecode

提问by blop

I'm trying to decode a JSON with jQuery. Here's what I get (for instance a class, here with one student):

我正在尝试使用 jQuery 解码 JSON。这是我得到的(例如一个班级,这里有一个学生):

"{"Students":[{"Name":John,"Grade":17,}],"TotalClass":17,"TotalCount":1,}"

here's what I do:

这就是我所做的:

$j.ajax({
    type: 'POST',
    url: 'class.aspx/getClass',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (msg) {
        $j.each(msg, function (index, element) {
            alert(element.TotalClass);
        });
    },
});

It keeps saying undefined in the alert (but I recieve the right JSON). Any idea what I'm doing wrong?

它一直在警报中说未定义(但我收到了正确的 JSON)。知道我做错了什么吗?

回答by Shyju

{"Students":[{"Name":John,"Grade":17,}],"TotalClass":17,"TotalCount":1,}

is not valid JSON !

不是有效的 JSON!

Assuming you have a valid JSONlike this

假设你有一个JSON这样的有效

{
    "Students": [
        {
            "Name": "John",
            "Grade": "17"
        }
    ],
    "TotalClass": " 17",
    "TotalCount": "1"
}

You can access the values like this

您可以像这样访问值

alert("TotalClass : "+msg.TotalClass);
//loop thru students
$.each(msg.Students,function(index,item){
   alert(item.Name+ " - "+item.Grade)
}); 

Working sample : http://jsfiddle.net/ncbLF/5/

工作示例:http: //jsfiddle.net/ncbLF/5/

Use jsonlintto validate JSON

使用jsonlint验证 JSON

So your code can be simplified to

所以你的代码可以简化为

$.getJSON("class.aspx/getClass",function(msg){

    alert("TotalClass : "+msg.TotalClass);
    $.each(msg.Students,function(index,item){
        alert(item.Name+ " - "+item.Grade)
    });
});

回答by thecodeparadox

Just try to alert

试着提醒一下

$j.each(msg, function (key, element) {
    alert(key); // output: Students, TotalClass..
    alert(element); //output: [{"Name":John,"Grade":17,}, 17..
});

Note

笔记

as you set dataType: 'json'so I think you don't need any additional parse effort and given JSON has error, don't know is it your written or originally sent from server.

正如您设置的dataType: 'json'那样,我认为您不需要任何额外的解析工作,并且鉴于 JSON 有错误,不知道它是您编写的还是最初从服务器发送的。

And you don't need the line

你不需要这条线

contentType: 'application/json; charset=utf-8',

You valid json should look like:

您的有效 json 应如下所示:

{
    "Students": [
        {
            "Name": "John",
            "Grade": "17"
        }
    ],
    "TotalClass": " 17",
    "TotalCount": "1"
}

回答by Rocket Hazmat

contentTypeis the type of the data sent tothe server, not from. Remove that.

contentType是发送服务器的数据类型,而不是来自。去掉那个。

The JSON you included in the question. Is that the exactJSON the server returns? Because if it is, you don't need the $.each. You have an object, you should only need $.eachto loop though an array of objects.

您在问题中包含的 JSON。那是服务器返回的确切JSON 吗?因为如果是这样,您就不需要$.each. 你有一个对象,你应该只需要$.each遍历一个对象数组。

So, just try alert(msg.TotalClass).

所以,试试吧alert(msg.TotalClass)

Also, that JSON is invalid. You have an extra ,after TotalCount, and after Grade. Also, Johnshould be in double quotes.

此外,该 JSON 无效。你有一个额外的,afterTotalCount和 after Grade。另外,John应该用双引号。