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
It looks like you're getting back an array. If it's always going to consist of just one element, you could do this (yes, it's pretty much the same thing as Tomalak's answer):
$.each(result[0], function(key, value){
console.log(key, value);
});
If you might have more than one element and you'd like to iterate over them all, you could nest $.each()
:
$.each(result, function(key, value){
$.each(value, function(key, value){
console.log(key, value);
});
});
$.each(result, function(key, value) {
console.log(key+ ':' + 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.