I'm not sure what are you trying to do with your Javascript function above, but if your goal is to produce JSON with such structure using Javascript, maybe you can try this :
var stringifyObj = JSON.stringify({
"addressAddressId": {
"addressId": 1,
"address": "Järnvägen 2",
"address2": null,
"district": null,
"postalCode": "20360",
"phone": null,
"coordinates": null,
"latitude": null,
"longitude": null,
"directions": null,
"description": null,
"addrZipCityCountry": null,
"lastUpdated": 1361754860000,
"cityId": {
"cityId": 1,
"city": "",
"lastUpdate": 1361754850000,
"countryCountryId": {
"countryId": 1,
"country": "Sweden",
"lastUpdate": 1361754837000
}
}
}
});
Basically, all you need to do is wrap the JSON string as a parameter for the JSON.stringify
.
Or, if you need to build the object incrementally, instead of providing all the properties at once like above, maybe you can try this :
var obj = {};
//add properties as needed
//simple properties
obj.addressId = 1;
obj.address = "Järnvägen 2";
obj.address2 = null;
//nested properties
obj.cityId = {};
obj.cityId.cityId = 1;
obj.cityId.countryCountryId = {};
obj.cityId.countryCountryId.countryId = 1;
After all the object properties are properly filled, pass it to the JSON.stringify
like this :
var stringifyObj = JSON.stringify(obj);
Or, to produce the JSON described on your example, you can further wrap the obj
like this :
var stringifyObj = JSON.stringify({ addressAddressId = obj });
I hope you get the idea :)