通过 AJAX 的 JSON 格式
嗨,我得到这样的 json 格式
{
"communication": [{
"communication_name": "None",
"communication_id": "1"
}],
"hardware": [{
"hardware_name": "XXXXXXXX",
"hardware_id": "6"
}],
"Sofware": [{
"software_name": "XXXXXX",
"software_id": "3"
}, {
"software_name": "XXXXXXXXXXXXX",
"software_id": "4"
}]
}
,但是当我在 ajax 中对此响应进行警报时,它显示为 [object Object] ajax代码是这样的
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var model_result = JSON.parse(xmlHttp.responseText)
alert('' + model_result);
}
我已经尝试过JSON.parse和eval。
hi i'm getting the json format like this
{
"communication": [{
"communication_name": "None",
"communication_id": "1"
}],
"hardware": [{
"hardware_name": "XXXXXXXX",
"hardware_id": "6"
}],
"Sofware": [{
"software_name": "XXXXXX",
"software_id": "3"
}, {
"software_name": "XXXXXXXXXXXXX",
"software_id": "4"
}]
}
but while i'm doing alert of this response in ajax it showing as [object Object]
the ajax code is like this
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var model_result = JSON.parse(xmlHttp.responseText)
alert('' + model_result);
}
I have tried both JSON.parse and eval.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试打印 JSON 对象的字符串化版本,如下所示:
alert(JSON.stringify(model_result));
You can try printing out the string-ified version of the JSON object like this:
alert(JSON.stringify(model_result));
如果您有带有 FireBug 的 FireFox,请编写
console.log (model_result);
或console.dir(model_result);
并且您自己确保返回的样子if you have FireFox with FireBug write
console.log (model_result);
orconsole.dir(model_result);
and you ensure yourself how return looks like解析后的 JSON 字符串是 javascript 中的对象。那是正常的。
例如,如果您想查看第一个 software_id,您可以这样做:
A parsed JSON string, is an object in javascript. Thats normal.
If for example, you want to see the first software_id you can do this:
这取决于您如何进行 AJAX 调用。大多数 API 会在收到字符串响应时对其进行 EVAL,从而将其转换为对象。如果您需要字符串,请确保您以文本形式调用,而不是 JSON。
在您的情况下,如果您想要字符串,请不要 JSON.parse 响应。这就是将其转换为对象的原因。
如果要显示属性的值,另一种方法是使用关联数组语法迭代对象
It depends how you are doing your AJAX call. Most API's EVAL the string response as they receive it, which turns it into an object. Make sure you are calling as TEXT and not JSON if you want the STRING.
In your case if you want the string, don't JSON.parse the response. This is what is converting it to an object.
If you want to display the values of the properties an alternative is to iterate over the object using associative array syntax