Hi All i have a json formatted out put as
{"baseUrl":"\/","success":true}
how get the value of success ??
Hi All i have a json formatted out put as
{"baseUrl":"\/","success":true}
how get the value of success ??
For this you might have to add the JSON lib for old browser versions:
var json = JSON.parse('{"baseUrl":"\/","success":true}');
// or
json = {"baseUrl":"\/","success":true};
alert( json.success )
//or
alert ( json['success'])
In jQuery ajax you can use the dataType json
.
This will parse the code directly so you would have
/* Ajax Get-Request */
$.ajax({
type : 'get',
url : "myurl.html",
dataType : 'json',
success : function ( response )
{
alert ( response.success )
alert ( response['success'])
},
// Internal Server Error / Timeout
error : function ( XMLHttpRequest, textStatus, errorThrown )
{
alert ( "Error \n" + textStatus );
}
});
Here ya go.
var getJsonProperty = (function(){
var hasJson = (window.JSON && JSON.parse && JSON.parse.call);
return hasJson ? function (jsonString, property) {
return JSON.parse(jsonString)[property];
} : function (jsonString, property) {
return new Function("return ("+jsonString+")['"+property+"'];")();
}
})();
alert (getJsonProperty('{"baseUrl":"\/","success":true}', 'success'));
// shows `true`