Question

EDIT: Something like this, but this is not working either, but there is the problem I think

var stringifyObj = JSON.stringify({
      "addressAddressId":$('#address').val(){ 
          "cityId:"$('#city').val(){
                "postalCode":$('#postalCode').val() 
           } 
      }
});     

*When I generate test client in Netbeans, JSON-structure (GET/JSON) is like that, but how can I code that with Javascipt and Strinfy-function? *

    "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
            }
        }
    },

QUESTION

  1. What is the correct syntax when using JSON.stringify in case of own object type like City-object inside of Address-object?
  2. Should I add every field to json if not using @JsonIgnoreProperties({""})? I just need address, city and postal code. address is type of Address with field String address in server side, City is type of City includes String-field for city name etc.
Was it helpful?

Solution

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 :)

OTHER TIPS

And you are missing a comma here, though I can't say that is your issue.

"postalCode": {
    "postalCode": $('#postalCode').val()
} <--- need comma here

"addressAddressId": {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top