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
To get key and value from json object in javascript, you can use Object. keys() , Object. values() , for Object. entries() method the methods helps you to get both key and value from json object.
A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma. The order of the key-value pair is irrelevant. A key-value pair consists of a key and a value, separated by a colon ( : ).
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); }); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With