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数据弹出一个alert ",希望它把JSON数据作为字符串提供给我.但javascript并不能做到这一点(这很好!),就像"嘿,这就是我们显示对象的方式,[对象]".

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判断.

Anyway, back to our regularly scheduled questions!


我试图通过ajax调用获取一些JSON数据,但jQuery似乎不喜欢我的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 (数据)中得到:

    {"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)给我提供了以下内容:

[object Object]

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

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.

推荐答案

函数alert()只能显示文本字符串.作为其唯一参数,它接受字符串或对象.The object will however be converted into a string that can be displayed.

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].

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

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

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

Json相关问答推荐

如何使用Laravel在MariaDB JSON kolumn中使用unicode字符

需要有关在Ffltter应用程序中解码JSON的帮助;未处理的异常:类型不是类型转换中类型的子类型

如果主对象中已存在属性,则不应在Jolt中引用次对象

解析Ansible AWS ec2_security_group中的json以提取安全组ID

用 Jolt 替换 JSON 中的值

迭代powershell双维json对象

Vega-Lite规范:尽管在规范中提供了数据,但显示空图表

匹配来自不同数组的值

从 PySpark 中的复杂 JSON 文件中高效清除 HTML 实体

使用 System.Text.Json 序列化记录成员

为什么在测试 RSPEC 时 JBuilder 不返回 JSON 中的响应正文

获取json中某个键的索引

解析包含换行符的 JSON

json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)

什么 Python 框架用于没有前端的 REST/JSON Web 服务?

将 PHP 结果数组转换为 JSON

在 JSON.NET 中序列化派生类时的字段顺序

JSON 模式 - 如果对象*不*包含特定属性则有效

有没有一种快速的方法可以在文本编辑器中将 JavaScript 对象转换为有效的 JSON?

如何从 jQuery ajax 调用将复杂对象传递给 ASP.NET WebApi GET?