JSON.Parse正在复制ID字段

时间:2012-06-11 20:21:53

标签: javascript jquery json

我从webservice传递以下JSON:

{
"selList": [
    {
        "id": "selBaMbReasonExempt",
        "value": "Confidential Contract|Customer in migration to automation|Missing contract|Non-English contract|Non-Standard way of ordering|Other (please specify in comments)|",
        "html": "Confidential Contract|Customer in migration to automation|Missing contract|Non-English contract|Non-Standard way of ordering|Other (please specify in comments)|",
        "key": null,
        "optRate": null,
        "optDate": null
    },
    {
        "id": "selBaMbInvoiceCurrency",
        "value": "BRL|",
        "html": null,
        "key": "BR|",
        "optRate": "0.57833555028627600000|",
        "optDate": "01-Feb-2012"
    },
    {
        "id": "selBaMbContractCurrency",
        "value": "BRL|USD|",
        "html": null,
        "key": "BR|BR|",
        "optRate": "0.57833555028627600000|1.00000000000000000000|",
        "optDate": "01-Feb-2012"
    }
    ]
}
function getSelLists() {
    var wkError = false;
    var serverResponse;
    var ss;
    MbrtUtil.disableAjaxNotifier();
    jQuery.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: "Webservices/TemplateBA.asmx/GetSelLists",
        data: "{'noOp': 'noOp'}",
        success: function (msg, status) {
            serverResponse = JSON.parse(msg);
            ss = msg;
        },
        error: function (request, status, error) {
            wkError = true;
        },
        complete: function (msg) {
            if (wkError == true) {
                alert('Error in getSelLists');
            }
            else {
                MbrtUtil.processServerData(serverResponse);
                if ('sdkList' in serverResponse) {
                    alert('selList is in serverResponse');
                    var selList = serverResponse.sdkList;
                    wklen = selList.length;
                    for (var i = 0; i < wklen; i++) {
                        //  if (selList[i].id == 'selBaCsInvoiceCountry') { MODba.invoiceCountryList = selList[i]; }
                        //   if (selList[i].id = 'selBaCsPaymentTerms') { MODba.paymentTermsList = selList[i]; }
                        if (selList[i].id = 'selBaMbInvoiceCurrency') { MODba.invoiceCurrencyList = selList[i]; }
                        if (selList[i].id = 'selBaMbContractCurrency') { MODba.contractCurrencyList = selList[i]; }

                        if (selList[i].id == 'selBaMbReasonExempt') {
                            alert('exempt list');
                            MODba.reasonExempt = selList[i];
                            if (wkOpt == 2) {
                                buildReasonExemptList();
                            }
                        }
                    }
                    var valList = MODba.reasonExempt.value.split("|");  //delete this
                    if (wkOpt == 1) {
                        buildRequestorLists();
                    }
                }
                MbrtUtil.enableAjaxNotifier();
            }
        }
    });
}

我通过JSONLint检查了语法。我有一个jQuery ajax调用webservice。完成后,该消息看起来像上面显示的代码。运行serverResponse = JSON.parse(msg)后,serverResponse包含三个selList对象。一切看起来都很好除了所有三个“id”字段包含“selBaMbContractCurrency”。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

比较时使用double'=':

if (selList[i].id == 'selBaMbContractCurrency')

在这种情况下,最好使用switch

for (var i = 0; i < wklen; i++) {
     switch(selList[i].id){
         case 'selBaMbInvoiceCurrency':
             MODba.invoiceCurrencyList = selList[i];
             break;
         case 'selBaMbContractCurrency':
             MODba.contractCurrencyList = selList[i];
             break;
         case 'selBaMbReasonExempt':
             alert('exempt list');
             MODba.reasonExempt = selList[i];
             if (wkOpt == 2) {
                 buildReasonExemptList();
             }
             break;
     }
}

它更具可读性和显而易见性。