-1

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.
Sami
  • 2,311
  • 13
  • 46
  • 80
  • How is `$('#address').val()` an "*own object type*"? Isn't it a simple string (or an array for selects)? – Bergi Feb 25 '13 at 02:07
  • 1
    Why there are two `addressAddressId` properties? – andri Feb 25 '13 at 02:12
  • @andri It is just a name. Netbeans generates that kind of stupid names when you have relations and oneToMany, manytToOne etc connections. addressAddressId is reference to object type Address, addressId is a field and primary key in Address-table. – Sami Feb 25 '13 at 09:45
  • @Bergi I haven't created a DB and there is a own table for Address, City, Country etc and then I've got JPA-entities for those as well. At the moment problem is that my syntax is wrong in JSON-stringify, I THINK! – Sami Feb 25 '13 at 09:46
  • @Sami: I'm not asking where the name comes from, i'm just asking why there are two properties with the same name. Are both generated by NetBeans? – andri Feb 25 '13 at 09:57
  • @andri My mistake, sorry! The second json-structure is just an example what I got from test client in GET. I updated the question. I just want to add new address, new city inside of it and new postal code, but I am so newbie in that Json - javaScipt-area. – Sami Feb 25 '13 at 10:15

2 Answers2

2

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

andri
  • 1,021
  • 1
  • 9
  • 16
  • Thanks! I think that your second choice is the best. I just tried to take values from the form (input fields like address etc.) and send/POST them to REST service and save them to database with JPA. The problem was the syntax when I tried to create JSON. I let you know how I managed to solve this later, too busy at the moment :) – Sami Feb 26 '13 at 08:28
  • Now I got it working. var user = {}; user.addressAddressId = {}; user.addressAddressId.address = $('#address').val(); user.addressAddressId.postalCode = $('#postalCode').val(); ... THANKS A LOT! BTW, how I manage the response in my web service? I would like to forward user to different page after success etc? – Sami Feb 26 '13 at 11:21
  • Glad to hear that you've got it working. For your question, maybe you can post that as another question here in SO :) – andri Feb 27 '13 at 07:48
  • I have to say that I HATE JS and my learning curve is flat :) http://stackoverflow.com/questions/15105968/how-to-handle-errors-in-client-server-application-using-rest-services Here is the continuous question... – Sami Feb 27 '13 at 07:54
0

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

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

"addressAddressId": {
james emanon
  • 11,185
  • 11
  • 56
  • 97
  • Sorry for that! My answer was badly structured. The second addressAddressId is just an example from GET/JSON with testCient in Netbeans. – Sami Feb 25 '13 at 10:37