2

我正在使用 Newtonsoft dll 从 xml 文件生成 json。从下面我将如何将地址详细信息放入列表中(如果示例中有更多)并将它们写入下拉列表我有以下有效的 json(检查 onjsonlint):

{
    "?xml": {
        "@version": "1.0",
        "@encoding": "utf-8"
    },
    "Root": {
        "Information": {
            "Error": {
                "ErrorNo": "0",
                "ErrorMsg": null
            },
            "Address": {
                "Address": [
                    {
                        "@AddressID": "14961943",
                        "@Sequence": "1",
                        "@Description": "Some Company Name, Some Building, 10 Some Street, Some County, Some City"
                    }            
                ]
            }
        }
    }
}
4

2 回答 2

1

试试这个:

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);
}

示例小提琴

于 2012-05-03T13:18:44.070 回答
0

不使用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);
}
于 2012-05-03T13:20:38.783 回答