Hi, I have an array (named "vehicles") containing some objects. I also have a string in JSON format called "settings" that contains some settings. var settings = ''{ "width": "200px", "height": "300px" }'' Now I want to put these two together into one beautiful JSON string so later when I tried to get the data I simply use: data = myString.evalJSON() data.settings.width data.vehicles[2].color How do I do this? I pretty bad at manipulate JSON strings... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Okey, I change the structure and found a solution. var settings = new Object(); settings.height = this.height; settings.width = this.width; var map = new Object(); map.settings = settings; map.vehicles = myVehiclesArray; var data = Object.toJSON(map).evalJSON(); alert(data.settings.width); alert(data.vehicles[2].color); Is this the best way to do it? :/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Fri, Mar 21, 2008 at 10:30 AM, mng0 <ggustav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> var settings = new Object(); > settings.height = this.height; > settings.width = this.width; > > var map = new Object(); > map.settings = settings; > map.vehicles = myVehiclesArray; > > var data = Object.toJSON(map).evalJSON(); > > alert(data.settings.width); > alert(data.vehicles[2].color); > > Is this the best way to do it? :/You''re definitely heading in the right direction! However, JSON stands for JavaScript Object Notation. It''s basically a way of representing JS objects as strings so they can be passed around. However, the statement "var map = new Object()" does just that: it makes a new Javascript object. Since it''s already a Javscript object, there''s no need to run toJSON() and then evalJSON() on it. What that does is take your Javascript object, convert it to a string, then parse that string to create another JS object--but the object you end up with is for all intents and purposes identical to the object you started with! It''s sort of like if you started with an integer, converted it to a string, then parsed the string to get the integer value back--a lot of unnecessary work! var i = 5; var j = parseInt(i.toString()); // ugh--why?! Incidentally, Javascript has a handy shortcut syntax for making objects, so you don''t have to call the Object constructor every time. Using your code as an example, it looks like this: var settings = { height: this.height, width: this.width }; var map = { settings: settings, vehicles: myVehiclesArray }; alert(map.settings.width); alert(map.vehicles[2].color); Simple, eh? And hey--if you flatten the right side of, say, the settings declaration, you get something that looks like this: { height: this.height, width: this.width }. Looks suspiciously like JSON, doesn''t it? Remember, that''s because JSON is already in Javascript syntax! Hope that helps! :Dan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
ahh! You got a really big point here! I''m so stupid! I already have 3 classes(objects) Base Class have property for Map, and map have a property for vehicles. Base this.name = ''Base''; this.map = new Map(); Map this.width = 10; this.height = 15; this.vehicles = new Array(); this.dimension = 10; Vehicle this.posY = 12; this.posX = 10; *** All I have to do when I save is just this simple method in Base class. save: function() { myData = Object.toJSON(this); //Send myData to Server with Ajax } I can only see one problem with this. When I save the map, I don''t want the map property ''dimension'' to exist in the JSON string cuz it''s final/readonly/static or whatever you call it. Is there an easy way to remove it? (Not that important). And one more question. The vehicles array in map class contains not just objects from vehicle. It''s also containing objects from a class that is inherits from vehicle. When I convert these objects to a JSON string I can''t see what instance each object in the array has. Is there anyway to solve this, or do I have to make a property for that in the vehicle class. Hope you understand my small issues, and thanks for the revelation! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Fri, Mar 21, 2008 at 11:32 AM, mng0 <ggustav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I can only see one problem with this. When I save the map, I don''t > want the map property ''dimension'' to exist in the JSON string cuz it''s > final/readonly/static or whatever you call it. Is there an easy way to > remove it? (Not that important).Yep, the "delete" keyword: delete map.dimension;> And one more question. The vehicles array in map class contains not > just objects from vehicle. It''s also containing objects from a class > that is inherits from vehicle. When I convert these objects to a JSON > string I can''t see what instance each object in the array has. Is > there anyway to solve this, or do I have to make a property for that > in the vehicle class.The easiest thing to do would be to add a ''type'' property to the Vehicle subclasses. :Dan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks again mate! When I convert the object to JSON I don''t want to include the dimension property, but neither do I want to delete it from the real object. I tried to use the Object.clone() function, but that was just shallow copy and I need deep copy? var myClone = myRealObject; that won''t work either, because it''s pointing to the same memory spot, right? No copy/clone. How do I do? :) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Fri, Mar 21, 2008 at 3:51 PM, mng0 <ggustav-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I tried to use the Object.clone() function, but that was just shallow > copy and I need deep copy? > > var myClone = myRealObject; > > that won''t work either, because it''s pointing to the same memory spot, > right? No copy/clone.Yeah, that''s right. As far as I know, there''s no built-in way to do deep copying with Javascript or Prototype; you''ll have to implement it yourself. That said, you could try what you were originally doing, that is: var copy = original.toJSON().evalJSON(); Since it''s getting converted to a string and then re-evaluated, it''s creating a brand new object, albeit one that is functionally identical to the original object. Then you could just do: delete copy.map.dimension; And then transmit the copy via AJAX. That said, I''m not sure it''s worth the bother. It''s probably easier just to ignore the dimension property on the server side. :Dan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---