在Jquery中设置嵌套JSON的值

时间:2011-07-28 00:58:25

标签: javascript jquery ajax json

我有一个类似于

的Json
{
    "Market": 0,
    "Marketer": null,
    "Notes": null,
    "SalesChannel": null,
    "ServiceLocations": [
        {
            "ExtensionData": null,
            "AdminFee": 0,
            "CommodityType": 0,
            "ContractType": 0,
            "Payment": {
                "ExtensionData": null,
                "BankAccountNumber": null,
                "BankAccountType": 0,
                "BankRoutingNumber": null,
                "CardType": 0,
                "CreditCardExpirationDate": "/Date(-62135575200000)/",
                "CreditCardNumber": null,
                "CreditCardReferenceID": null,
                "CreditCardSecurityCode": null,
                "PaymentAddress1": null,
                "PaymentAddress2": null,
                "PaymentAmount": 0,
                "PaymentCity": null,
                "PaymentCounty": null,
                "PaymentFirstName": null,
                "PaymentLastName": null,
                "PaymentPhone": null,
                "PaymentState": null,
                "PaymentType": 0,
                "PaymentZip": null
            },
            "ProductID": null,
            "PromoCode": null,
            "Rate": 0,
            "RateSchedule": null,
            "ServiceAddress1": null,
            "ServiceAddress2": null,
            "ServiceCity": null
        }
    ],
    "SocialSecurityNumber": null,
    "SubAgent": null,
    "Login": null
}

我想动态循环遍历JSON并设置每个键值对的值。如果值存在,则设置值,但如果该值不存在则保持原样。

我正在做这样的事情,因为这似乎不起作用

$.getJSON("data.js",function buildjson(json){
                myJSONObject = json;
                        for (var key in myJSONObject) {
                                    if(myJSONObject.hasOwnProperty(key)){
                                    if(typeof(myJSONObject[key]) === 'object')
                                    {
                                    newJson(myJSONObject[key]);
                                    }

                                    if($("#main").find(":input[name="+key+"]") != "") {
                                    modifyJson(key,myJSONObject[key]);
                                    }
                                    else{
                                    setData(key,myJSONObject[key]);
                                    }
                                    $("#keylist").append(key+":"+myJSONObject[key]+"<br />");
                                    //newJson(myJSONObject[key]);
                                }
                    }
                function newJson(myJSONObject) {
                    for (var key in myJSONObject) {
                                        if(myJSONObject.hasOwnProperty(key)){
                                        modifyJson(key,myJSONObject[key]);
                                        newJson(myJSONObject[key]);
                                    }
                        }
        }
        function setData(path, value) {
    if (path.indexOf('.') != -1) {
        path = path.split('.');
        for (var i = 0, l = path.length; i < l; i++) {
            if (typeof(data[path[i]]) === 'object') {
                continue;
            } else {
                data[path[i - 1]][path[i]] = value;
            }
        }
    } else {
        data[path] = value;
    }
};

            function modifyJson(key,value) {
                var theValue = value;
                //$("#keylist1").append(key+":"+theValue+"<br />");
                if($("#main").find(":input[name="+key+"]")) {
                    theValue = $(":input[name="+key+"]").val();
                    setData(key,theValue);
                }
            }

    });

    }

我需要做哪些修改?

1 个答案:

答案 0 :(得分:0)

"ServiceCity": null之后有一个尾随逗号,导致无效的JSON对象。