jQuery.getJSON和jQuery.parseJSON返回[object Object]吗? [英] jQuery.getJSON and jQuery.parseJSON return [object Object]?

查看:146
本文介绍了jQuery.getJSON和jQuery.parseJSON返回[object Object]吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我已经获得了有关该问题的著名问题"徽章,所以我想我会回过头来,将发生的事情贴在对人们最尖锐的顶部搜索它以立即获得答案.

EDIT: I've gotten the "famous question" badge with this question, so I figured I'd come back to it and stick what happened to me right at the very tippy top for people searching it to get an answer right away.

基本上,我是JSON的新手. JSON是一个对象(很明显),因为它包含各种东西!因此,我就像嘿,JavaScript,只是弹出带有所有这些JSON数据的警报",期望它以字符串形式提供给我JSON数据.但是javascript并不能做到这一点(很好!),所以就像嘿,这就是我们显示对象的方法,[object Object]".

Basically, I was new to JSON. JSON is an object (obviously), as it contains all kinds of stuff! So I was like "Hey, javascript, just pop up an alert with all of this JSON data", expecting it to give me the JSON data as a string. But javascript doesn't do that (which is good!), so it was like "Hey, this is how we display objects, [object Object]".

我能做的是像alert(obj.DATA[0][1])这样的东西,它将向我显示该对象的那一部分.

What I could've done is something like alert(obj.DATA[0][1]) and it would've shown me that bit of the object.

我真正想要的是验证我是否制作了良好的JSON数据,可以使用JSON.stringify进行检查.

What I really wanted was to verify that I was making good JSON data, which I could've checked with JSON.stringify.

无论如何,回到我们定期安排的问题!

Anyway, back to our regularly scheduled questions!

我正在尝试通过ajax调用获取一些JSON数据,但是jQuery似乎并不喜欢我的JSON.

I'm trying to get some JSON data with an ajax call, but jQuery doesn't seem to like my JSON.

如果我做类似的事情:

function init2() {
    alert("inside init2");
    jQuery.ajax({
        url: "/Mobile_ReportingChain.cfm",
        type: "POST",
        async: false,
        success: function (data) {
            alert(data);
            var obj = jQuery.parseJSON(data);
            alert(obj);
        }
    });
}

我是从alert(data)获取的

I get this as from alert(data):

    {"COLUMNS":["MFIRST_NAME","MLAST_NAME","MMDDL_NAME","MEMPLY_ID","MAIM_NBR","EMPLY_ID"],
    "DATA":[

["FNAME1          ","LNAME1                  ","MI1              ","000-14-7189","026-0010","000-62-7276"]      

,["FNAME2           ","LNAME2                    ","MI2              ","000-01-2302","101-1850","000-14-7189"]  

,["FNAME3           ","LNAME3                  ","MI3              ","000-91-3619","102-1000","000-01-2302"]    

,["FNAME4         ","LNAME4                  ","MI4              ","000-25-9687","102-1000","000-91-3619"]  

]}  

JSONLint所说的

是有效的json. alert(obj)给了我这个,但是:

which JSONLint says is valid json. alert(obj) gives me this, however:

[object Object]

添加dataType: "json""text json"只会使其在alert(data)处报告[object Object].

adding dataType: "json" or "text json" just makes it report [object Object] at alert(data).

我真的很想弄明白这一点,有人知道为什么这样做吗?我是jQuery的新手,我的目标是为每个列获取一个数组.我正在使用的相同代码在不同的页面上运行,这让我最困扰.

I'd really like to get this figured out, does anyone know why it's doing this? I'm pretty new at jQuery, my goal is to get an array for each of the columns. The same code I'm using has worked on a different page it looks like, which is what's bothering me the most.

推荐答案

通过jQuery提取JSON时,$.ajax()方法将自动解析JSON并将其转换为JavaScript对象.为此,您的data变量是一个JavaScript对象,而不是人们期望的JSON字符串.

When fetching JSON through jQuery, the $.ajax() method will automatically parse the JSON and turn it into a JavaScript object for you. Your data variable is therefor a JavaScript object, and not a JSON string as one might expect.

由于alert()仅可以显示字符串,因此当尝试警告您的data对象时,您的对象将变成其字符串表示形式. JavaScript对象的字符串表示形式是[object Object].

Since alert() only can display strings, when trying to alert your data object, your object will be turned into its string representation. The string representation of a JavaScript object is [object Object].

出于调试目的,可以改用console.log(data).然后,您可以通过浏览器开发人员工具中的控制台检查对象及其内容.

For debug-purposes you can use console.log(data) instead. You can then inspect the object and its content through the console in your browsers developer tools.

function init2() {
    jQuery.ajax({
        url: "/Mobile_ReportingChain.cfm",
        type: "POST",
        dataType: "json",
        async: false,
        success: function (data) {
            console.log(data);
        }
    });
}

如果出于某种原因您仍然想警告JSON数据,则必须将data对象重新转换为JSON字符串.为此,您可以使用 JSON.stringify :

If you for some reason still want to alert the JSON-data, then you would have to turn your data object back into a JSON-string. To do that you can make use of JSON.stringify:

alert(JSON.stringify(data));

这篇关于jQuery.getJSON和jQuery.parseJSON返回[object Object]吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆