JSON format via AJAX
hi i'm getting the json format like this
{
"communication": [{
"communication_开发者_如何学Goname": "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.
You can try printing out the string-ified version of the JSON object like this:
alert(JSON.stringify(model_result));
if you have FireFox with FireBug write console.log (model_result);
or console.dir(model_result);
and you ensure yourself how return looks like
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:
alert(model_result.Software[0].software_id);
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
for(var i in resultObject) {
var value = resultObject[i];
alert(i + " = "+ value);
}
精彩评论