Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 webservice: enter image description here

Please help me to print Key and it's Value

like image 649
imdadhusen Avatar asked Aug 16 '11 05:08

imdadhusen


People also ask

How do I get the key of a JSON object?

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.

What is key and value in JSON?

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 ( : ).


1 Answers

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);     }); }); 
like image 153
no.good.at.coding Avatar answered Sep 22 '22 09:09

no.good.at.coding