How to get JSON key and cost?

I wrote the following code to get the 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 the answer from webservice : enter image description here

Please help me print the key and its meaning.

+43
json jquery
Aug 16 2018-11-11T00:
source share
3 answers

It looks like you are returning an array. If it always consists of just one element, you can do it (yes, this is almost the same as Tomalakโ€™s answer):

 $.each(result[0], function(key, value){ console.log(key, value); }); 

If you can have multiple elements and want to $.each() over all of them, you can $.each() :

 $.each(result, function(key, value){ $.each(value, function(key, value){ console.log(key, value); }); }); 
+86
Aug 16 2018-11-11T00:
source share
 $.each(result, function(key, value) { console.log(key+ ':' + value); }); 
+11
Aug 16 '11 at 5:10
source share

First, I see that you are using explicit $.parseJSON() . If this is due to the fact that you manually serialize JSON on the server side, do not. ASP.NET automatically converts JSON to the value of the returned method , and jQuery will automatically deserialize it for you.

To iterate over the first element in an array that you have, use the following code:

 var firstItem = response.d[0]; for(key in firstItem) { console.log(key + ':' + firstItem[key]); } 

If there is more than one element (it's hard to tell from this screenshot), you can iterate over response.d and then use this code inside this outer loop.

+9
Aug 16 2018-11-11T00:
source share



All Articles