我有两种类型的json数据。首先,只有一个元素将被插入到下拉列表中,而其他多个项目可以插入到下拉列表中
JS:
var data =
{
"GETBILLERRESPONSE":
{
"RESPONSECODE":"0","RESPONSEMESSAGE":"Success","BILLERS":
{
"BILLER":{
"@ID":"6","@EIDENFORCED":"FALSE","@ISGETBALANCE":"TRUE","#text":"POP CORN MANIA","SERVICES":null
}
}
}
};
var data1 = {
"GETBILLERRESPONSE":
{
"RESPONSECODE":"0","RESPONSEMESSAGE":"Success","BILLERS":
{
"BILLER":
[
{
"@ID":"1","@EIDENFORCED":"TRUE","@ISGETBALANCE":"TRUE","#text":"Mumbai Bill Payment","SERVICES":{
"SERVICE":
[
{
"@ID":"1","#text":"Bill Payment"
},
{
"@ID":"2","#text":"Fine Payment"
}
]
}
},
{
"@ID":"7","@EIDENFORCED":"FALSE","@ISGETBALANCE":"TRUE","#text":"Delhi Bill Payment","SERVICES":null
},
{
"@ID":"23","@EIDENFORCED":"FALSE","@ISGETBALANCE":"TRUE","#text":"Chennai Bill Payment","SERVICES":null
}
]
}
}
};
var ResponseMessage = data1.GETBILLERRESPONSE.RESPONSEMESSAGE;
if (ResponseMessage == "Success")
{
var k=0;
var Html = "<option value=\"-1\">" + "Please select a payee." + "</option>";
var lenforbillers = data1.GETBILLERRESPONSE.BILLERS.BILLER.length;
var objforbillers = data1.GETBILLERRESPONSE.BILLERS.BILLER;
for (k = 0; k < lenforbillers; k++)
{
Html += "<option value=\"" + (objforbillers[k]['@ID']) + "\">" + (objforbillers[k]['#text']) + "</option>";
}
$("#dlInstantPayPayeee").html(Html);
}
我的问题:
data1
但不适用于data
,因为data
只包含一个元素答案 0 :(得分:2)
1)如果json嵌套列表中只有一个元素,为什么我不能通过使用数组索引[0]来检索它?
因为该对象上没有名称为"0"
的属性。
2)我的for循环代码适用于data1,但不适用于数据,因为数据只包含一个元素
3)我怎样才能只编写一段代码,这些代码将放入下拉列表,而不管json中的嵌套元素
好吧,我会在源头修复它。无论它是返回一个还是几个都不重要,它应该返回一致的数据结构。
但是,如果你不能依赖它,你必须检查你是否回来了一个阵列:
if ($.isArray(data1.GETBILLERRESPONSE.BILLERS.BILLER) {
// It's an array
}
else {
// It's not
}
然后你可以采取相应的行动。一个简单的答案是使它成为一个数组:
if (ResponseMessage == "Success")
{
var k=0;
var Html = "<option value=\"-1\">" + "Please select a payee." + "</option>";
var objforbillers = data1.GETBILLERRESPONSE.BILLERS.BILLER;
if (!$.isArray(objforbillers)) { // <== New
objforbillers = [objforbillers]; // <== New
} // <== New
var lenforbillers = objforbillers.length; // <== Moved and modified
for (k = 0; k < lenforbillers; k++)
{
Html += "<option value=\"" + (objforbillers[k]['@ID']) + "\">" + (objforbillers[k]['#text']) + "</option>";
}
$("#dlInstantPayPayeee").html(Html);
}
或者,将for
循环的主体分解出来(在这种情况下,它是一个单行,但在一般情况下它可能不是),然后在适当时重复使用它:
if (ResponseMessage == "Success")
{
var k=0;
var Html = "<option value=\"-1\">" + "Please select a payee." + "</option>";
var objforbillers = data1.GETBILLERRESPONSE.BILLERS.BILLER;
var lenforbillers;
if ($.isArray(objforbillers)) {
lenforbillers = objforbillers.length;
for (k = 0; k < lenforbillers; k++)
{
Html += optionHtml(objforbillers[k]);
}
}
else {
Html += optionHtml(objforbillers);
}
$("#dlInstantPayPayeee").html(Html);
}
function optionHtml(entry) {
return "<option value=\"" + entry['@ID'] + "\">" + entry['#text'] + "</option>";
}
答案 1 :(得分:1)
缺少您的第一个数据语法,
"BILLER":
[
{
"@ID":"6","@EIDENFORCED":"FALSE","@ISGETBALANCE":"TRUE","#text":"POP CORN MANIA","SERVICES":null
}
]
答案 2 :(得分:1)
如果您将第一个data
对象更改为此
var data = {
"GETBILLERRESPONSE": {
"RESPONSECODE":"0","RESPONSEMESSAGE":"Success","BILLERS": {
"BILLER": [ {
"@ID":"6","@EIDENFORCED":"FALSE","@ISGETBALANCE":"TRUE","#text":"POP CORN MANIA","SERVICES":null
} ]
}
}
};
比你的片段在两种情况下都能正常工作;这意味着您当前的对象具有不同的结构。您的BILLER
对象data
不是正确的数组,因此您无法通过索引访问内容。