How to retrieve key and value from JSON String How to retrieve key and value from JSON String json json

How to retrieve key and value from JSON String


Use the for...in statement to iterate over keys:

for ( var k in someObject.supplier ) {    someOption.value = k;    someOption.text  = someObject.supplier[ k ];}


If I understood the question correctly, I think you might want to use something along this:

var jsonArray = ... ; // {"supplier": ... } (JS Object)var supplierOptions = jsonArray.supplier;// TODO: remember to set up selectme!for(var optionId in supplierOptions) {    if(!supplierOptions.hasOwnProperty(optionId)) {        continue;    }    var optionValue = supplierOptions[optionId];    // operate with name and value here (append to select ie.)    $("#selectme").append('<option id="' + optionId + '">' + optionValue + '</option>');}

The code probably could use some cleaning up. Hopefully it conveys the basic idea, though.