How to get values of attributes from JSON using jQuery

后端 未结 2 1674
误落风尘
误落风尘 2021-01-23 04:57

I am generating json from an xml file using the Newtonsoft dll. From the below how would I get the address details into a list(if there were more in the example) and write them

相关标签:
2条回答
  • 2021-01-23 05:10

    Solution without use of jQuery:

    var select = document.getElementById('selectID');
    var addresses = json.Root.Information.Address.Address;
    
    for(var i = 0, l = addresses.length; i < l; i++) {
      var o = document.createElement('option');
      o.value = addresses[i]['@AddressID'];
      o.innerHTML = addresses[i]['@Description'];
      select.appendChild(o);
    }
    
    0 讨论(0)
  • 2021-01-23 05:17

    Try this:

    var json = // that object above
    var addresses = json.Root.Information.Address.Address;
    
    for (var i = 0; i < addresses.length; i++) {
        var $option = $("<option></option>").val(addresses[i]["@AddressID"]).text(addresses[i]["@Description"]);
        $("#mySelect").append($option);
    }
    

    Example fiddle

    0 讨论(0)
提交回复
热议问题