Wednesday 18 December 2013

How to get JSON Key and Value?

Ref LINK http://stackoverflow.com/questions/7073837/how-to-get-json-key-and-value



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 webserviceenter image description here
Please help me to print Key and it's Value

3 Answers



up vote30down voteaccepted
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);
    });
});

Method : 2

$.each(result, function(key, value) {
  console.log(key+ ':' + value);
}

No comments:

Post a Comment