我有一个json如下
{"DataList":
{"Data":[
{"@attributes":{"description":"FGFGFGH","code":"FGFGFGH"}},
{"@attributes":{"description":"UIS default relying party","code":"UIS"}},
{"@attributes":{"description":"MDEx Healthcare Portal","code":"MDE"}},
{"@attributes":{"description":"VBK","code":"VBK"}},
{"@attributes":{"description":"Omnicare","code":"OC"}},
{"@attributes":{"description":"MDEX","code":"MDEX"}},
{"@attributes":{"description":"Bank of America","code":"BOA"}},
{"@attributes":{"description":"LDAP","code":"LDAP"}},
{"@attributes":{"description":"JetBlue","code":"JB"}},
{"@attributes":{"description":"Digital Signature Demo Application","code":"DEMODS"}}
]
}
}
我在jquery代码下面创建选择框选项
$.each(response, function(i, DataList) {
$.each(DataList,function(j,Data){
var finalObj = Data.@attributes;
options += '<option value="' + finalObj.code + '">' + finalObj.description + '</option>';
});
$("#relyingpartyall").html(options);
});
上面的代码在firefox 3.6中工作正常,但在firefox 10,11,chrome,safari中,即它无法正常工作,它会产生以下错误。
Firefox 11 : can't convert AttributeName to string ,var finalObj = Data.@attributes;
Chrome : Unexpected Identifier @ line "options += '<option value="' + finalObj.code + '">' + finalObj.description + '</option>';"
有人可以帮我解决这个问题吗?
答案 0 :(得分:5)
试试这样:
var finalObj = Data["@attributes"];
或直接访问单个$.each
中的数据数组:
var relyingpartyall = $('#relyingpartyall');
relyingpartyall.empty();
$.each(response.DataList.Data, function(i, item) {
var attributes = item["@attributes"];
relyingpartyall.append(
$('<option/>', {
value: attributes.code,
html: attributes.description
})
);
});