jquery - 如何像对象一样操作Json响应?

标签 jquery ajax json

我的 jQuery.ajax 返回 JSon 对象。我首先阅读了其他文章。但他们的回复文字不喜欢我的。 我的回复内容:来自 firebug 回复

{"item":"[{\"country\":\"USA\",\"lan\":\"EN\"},{\"country\":\"Turkiye\",\"lan\":\"TR\"}]"}

现在我试图提醒countryName:

$('#loadData').click(function() {
            $.ajax({
                type: "POST",
                url: "WS/myWS.asmx/getDaa",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                $("#jsonResponse").html(msg);
                    $.each(msg.item, function(i, d) {
                        alert(this.country);
                        debugger;
                    });
                },
            });
        });

但它正在警告“未定义”

最佳答案

item 的值是一个字符串。因此,您首先需要将其解析为 json。试试这个。

$("#jsonResponse").html(msg);
    var item = jQuery.parseJSON(msg.item)
    $.each(item, function(i, d) {
        alert(this.country);
        debugger;
    });
},

关于jquery - 如何像对象一样操作Json响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2454952/

相关文章:

jquery - jstree 杀死实时链接

javascript - 完整日历 GetEvents() 方法调用不起作用

python - GCP 服务帐户 key 轮换

javascript - 使用 jquery 数据表时出现 JSON 错误

c# - 如何将下面的JSON数组转换成IDictionary<string, object>?

javascript - 单击时淡出,除非单击输入元素

jquery - Jquery 可以知道我的 gif 动画何时结束吗?

javascript - Django 获取优惠券代码并检查该代码另一列中的 bool 值 ("is available")

javascript - 尝试对 PHP 进行 ajax POST 调用以将 SQL 查询变量返回给 jquery 时出错

javascript - 拔掉网线时抛出DOM异常