0

我正在从 Web 服务传递以下 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 检查了语法。我有一个对 web 服务的 jQuery ajax 调用。完成后,消息看起来像上面显示的代码。运行 serverResponse = JSON.parse(msg) 后,serverResponse 包含三个 selList 对象。除了所有三个的“id”字段包含“selBaMbContractCurrency”之外,一切看起来都很好。有任何想法吗?

4

1 回答 1

3

比较时使用双“=”:

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

它更具可读性和明显性。

于 2012-06-11T20:34:56.810 回答