Konstantin Mokhov Konstantin Mokhov - 2 months ago 9
Javascript Question

get variable from json response

I have a GET function and response in JSON.
This is function:

$.ajax({
contentType: 'application/json',
dataType: 'json',
success: function(msg){
var result = JSON.stringify(msg);
console.log(result);
},
error: error,
type: 'GET',
url: myurl
});


success response from 'result' is like this
{"response":{"values":[{"name":"john","sex":"male"}]}}


But when I try to get the 'name' from response, I get an error!
This is the error:

for(var k in result){
console.log(result[k]);
}


displays the letters

I tried this:

$.each(result,function(index, value){
console.log(index, value);
});


but always get error.

Answer

Try removing the JSON.stringify(msg.response). After which, msg.response.values[0].name should givejohn