开发者

setting values for nested JSON in Jquery

I have a Json which looks like following

{
    "Market": 0,
    "Marketer": null,
    "Notes": null,
    "SalesC开发者_StackOverflow社区hannel": 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
}

I want to dynamically loop through the the JSON and set the values of each key value pairs. if the value is present set the value but if the value is not present then leave it as is.

im doing something like this because this doesnt seem to work

$.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);
                }
            }

    });

    }

What modification do i need to do?


You have a trailing comma after "ServiceCity": null which is causing an invalid JSON object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜