I have written following code to get JSON result from webservice.
function SaveUploadedDataInDB(fileName) {
$.ajax({
type: "POST",
url: "SaveData.asmx/SaveFileData",
data: "{'FileName':'" + fileName + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var result = jQuery.parseJSON(response.d);
//I would like to print KEY and VALUE here.. for example
console.log(key+ ':' + value)
//Addess : D-14 and so on..
}
});
}
Here is response from webservice:
Please help me to print Key and it's Value
First, I see you're using an explicit $.parseJSON()
. If that's because you're manually serializing JSON on the server-side, don't. ASP.NET will automatically JSON-serialize your method's return value and jQuery will automatically deserialize it for you too.
To iterate through the first item in the array you've got there, use code like this:
var firstItem = response.d[0];
for(key in firstItem) {
console.log(key + ':' + firstItem[key]);
}
If there's more than one item (it's hard to tell from that screenshot), then you can loop over response.d
and then use this code inside that outer loop.
$.each(result, function(key, value) {
console.log(key+ ':' + value);
});
Source: Stackoverflow.com