Using Proto 1.5.1: Let''s say my server side code returns a JSON string with is an Array of hashes (with name-value pairs), such as in this case, where I return an array of 3 hashes via an Ajax.Request invocation: [{"VIDID":3359,"VIDPATHSTR":"/SS/vids/123","VIDDESCRIP":"test22"},{"VIDID":3358,"VIDPATHSTR":" /SS/vids/123","VIDDESCRIP":"some other descrip"}] While the following works very nicely for a single JSON object returned... doesn''t work directly on an array... var json = transport.responseText.evalJSON(true); In Prototype, what''s the easiest way to turn that returned JSON string of an array of hashes into an object that''s accessible via name/value pairs in my function? Many thanks for any words of wisdom! cheers, Mark --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
donkey.ear-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2007-Jun-07 23:31 UTC
Re: JSON Array of hashes
I use this var obj = eval( "(" + t.responseText + ")" ); Hope that helps; On Jun 8, 12:18 am, "Mark Holton" <holto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Using Proto 1.5.1: > > Let''s say my server side code returns a JSON string with is an Array of > hashes (with name-value pairs), such as in this case, where I return an > array of 3 hashes via an Ajax.Request invocation: > [{"VIDID":3359,"VIDPATHSTR":"/SS/vids/123","VIDDESCRIP":"test22"},{"VIDID":3358,"VIDPATHSTR":" > /SS/vids/123","VIDDESCRIP":"some other descrip"}] > > While the following works very nicely for a single JSON object returned... > doesn''t work directly on an array... > var json = transport.responseText.evalJSON(true); > > In Prototype, what''s the easiest way to turn that returned JSON string of an > array of hashes into an object that''s accessible via name/value pairs in my > function? > > Many thanks for any words of wisdom! > cheers, > Mark--~--~---------~--~----~------------~-------~--~----~ 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. Unfortunately, that works the same as what I was trying with "var jsonarray = transport.responseText.evalJSON(true);". ...I know the string passed back is:> [{"VIDID":3359,"VIDPATHSTR":"/SS/vids/123","VIDDESCRIP":"test22"},{"VIDID":3358,"VIDPATHSTR":"/SS/vids/123","VIDDESCRIP":"some > other descrip"}] >When I display the alert below, I receive the message: [object Object]>When I try to access an element of that array, via alert(jsonarray[1])... I return:> undefined >new Ajax.Request(url, { method: ''post'', parameters: pars, onComplete: function(transport) //get from JSON string { var jsonarray = transport.responseText.evalJSON(true); alert(jsonarray); var item = jsonarray[1]; alert(item); } } ); On 6/7/07, donkey.ear-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org <donkey.ear-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > > I use this > > var obj = eval( "(" + t.responseText + ")" ); > > Hope that helps; > > On Jun 8, 12:18 am, "Mark Holton" <holto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Using Proto 1.5.1: > > > > Let''s say my server side code returns a JSON string with is an Array of > > hashes (with name-value pairs), such as in this case, where I return an > > array of 3 hashes via an Ajax.Request invocation: > > > [{"VIDID":3359,"VIDPATHSTR":"/SS/vids/123","VIDDESCRIP":"test22"},{"VIDID":3358,"VIDPATHSTR":" > > /SS/vids/123","VIDDESCRIP":"some other descrip"}] > > > > While the following works very nicely for a single JSON object > returned... > > doesn''t work directly on an array... > > var json = transport.responseText.evalJSON(true); > > > > In Prototype, what''s the easiest way to turn that returned JSON string > of an > > array of hashes into an object that''s accessible via name/value pairs in > my > > function? > > > > Many thanks for any words of wisdom! > > cheers, > > Mark > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
aha, found it. the toSource() method of object does the trick. (is there a way in Proto that''s better?) ...> var jsonarray = transport.responseText.evalJSON(true); > var test = jsonarray.toSource(); //returns my array of hashes > alert(test); >... Thanks once again for the sounding board ...(seems to usually work when debugging) cheers, all- On 6/7/07, Mark Holton <holtonma-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Thanks. Unfortunately, that works the same as what I was trying with "var > jsonarray = transport.responseText.evalJSON(true);". > > ...I know the string passed back is: > > > [{"VIDID":3359,"VIDPATHSTR":"/SS/vids/123","VIDDESCRIP":"test22"},{"VIDID":3358,"VIDPATHSTR":"/SS/vids/123","VIDDESCRIP":"some > > other descrip"}] > > > > When I display the alert below, I receive the message: > > [object Object] > > > > When I try to access an element of that array, via alert(jsonarray[1])... > I return: > > > undefined > > > > new Ajax.Request(url, > { > method: ''post'', > parameters: pars, > onComplete: function(transport) //get from JSON string > { > var jsonarray = transport.responseText.evalJSON(true); > alert(jsonarray); > var item = jsonarray[1]; > alert(item); > } > } > ); > > On 6/7/07, donkey.ear-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org < donkey.ear-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > > > I use this > > > > var obj = eval( "(" + t.responseText + ")" ); > > > > Hope that helps; > > > > On Jun 8, 12:18 am, "Mark Holton" <holto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Using Proto 1.5.1: > > > > > > Let''s say my server side code returns a JSON string with is an Array > > of > > > hashes (with name-value pairs), such as in this case, where I return > > an > > > array of 3 hashes via an Ajax.Request invocation: > > > > > [{"VIDID":3359,"VIDPATHSTR":"/SS/vids/123","VIDDESCRIP":"test22"},{"VIDID":3358,"VIDPATHSTR":" > > > /SS/vids/123","VIDDESCRIP":"some other descrip"}] > > > > > > While the following works very nicely for a single JSON object > > returned... > > > doesn''t work directly on an array... > > > var json = transport.responseText.evalJSON(true); > > > > > > In Prototype, what''s the easiest way to turn that returned JSON string > > of an > > > array of hashes into an object that''s accessible via name/value pairs > > in my > > > function? > > > > > > Many thanks for any words of wisdom! > > > cheers, > > > Mark > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
This document http://www.w3schools.com/jsref/jsref_toSource_math.asp suggests that toSource is not available on IE either. Could someone else identify the best way to get an array of hashes (which is returned as a string from an ajax call) and then get something you can iterate? Take for example, returning a hash of ''rows'' to display, or maybe a dynamic update of a form metadata (what fields are visible/required), often you need to return a list/array of objects. I''ve also used the manual eval code before as I wasn''t returning it in a json header (just in the body) but it seems rather.. messy. So basically, i''m asking what Mark is asking- Mark; how did toSource help? according to the documentation url above, that returns the source, so it would be alerting a string representation of your var test- not an array. Gareth On 6/8/07, Mark Holton <holtonma-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > aha, found it. the toSource() method of object does the trick. (is there > a way in Proto that''s better?) > > ... > > > var jsonarray = transport.responseText.evalJSON(true); > > var test = jsonarray.toSource(); //returns my array of hashes > > alert(test); > > > ... > > Thanks once again for the sounding board ...(seems to usually work when > debugging) > > cheers, all- > > > On 6/7/07, Mark Holton <holtonma-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Thanks. Unfortunately, that works the same as what I was trying with > > "var jsonarray = transport.responseText.evalJSON(true);". > > > > ...I know the string passed back is: > > > > > [{"VIDID":3359,"VIDPATHSTR":"/SS/vids/123","VIDDESCRIP":"test22"},{"VIDID":3358,"VIDPATHSTR":"/SS/vids/123","VIDDESCRIP":"some > > > other descrip"}] > > > > > > > When I display the alert below, I receive the message: > > > > [object Object] > > > > > > > When I try to access an element of that array, via > > alert(jsonarray[1])... I return: > > > > > undefined > > > > > > > new Ajax.Request(url, > > { > > method: ''post'', > > parameters: pars, > > onComplete: function(transport) //get from JSON string > > { > > var jsonarray = transport.responseText.evalJSON > > (true); > > alert(jsonarray); > > var item = jsonarray[1]; > > alert(item); > > } > > } > > ); > > > > On 6/7/07, donkey.ear-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org < donkey.ear-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > > > > > > I use this > > > > > > var obj = eval( "(" + t.responseText + ")" ); > > > > > > Hope that helps; > > > > > > On Jun 8, 12:18 am, "Mark Holton" < holto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Using Proto 1.5.1: > > > > > > > > Let''s say my server side code returns a JSON string with is an Array > > > of > > > > hashes (with name-value pairs), such as in this case, where I return > > > an > > > > array of 3 hashes via an Ajax.Request invocation: > > > > > > > [{"VIDID":3359,"VIDPATHSTR":"/SS/vids/123","VIDDESCRIP":"test22"},{"VIDID":3358,"VIDPATHSTR":" > > > > /SS/vids/123","VIDDESCRIP":"some other descrip"}] > > > > > > > > While the following works very nicely for a single JSON object > > > returned... > > > > doesn''t work directly on an array... > > > > var json = transport.responseText.evalJSON(true); > > > > > > > > In Prototype, what''s the easiest way to turn that returned JSON > > > string of an > > > > array of hashes into an object that''s accessible via name/value > > > pairs in my > > > > function? > > > > > > > > Many thanks for any words of wisdom! > > > > cheers, > > > > Mark > > > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hey Gareth, howzit? :-) Gareth Evans a écrit :> Could someone else identify the best way to get an array of hashes > (which is returned as a string from an ajax call) and then get something > you can iterate?So if I get you right, you''re saying that the following responseText (just abbreviating with ''...'' in there): ''[ { hash1... }, { hash2... } ...]'' won''t get eval''d properly when you do xhr.responseText.evalJSON(). Correct? What if you tried to wrap it in an extra object with a single property, something like: ''{ data: [ { hash1... }, { hash2... } ...] }'' And then you''d access the data property of the eval''d object? Would that work for you? I''m sorry I don''t have time to test this just now, but I thought I''d suggest a possible lead. ''HTH -- Christophe Porteneuve aka TDD tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Not too bad, FYI in NZ so its 9:45 here- premium coding time... if I could just be bothered to start up my dev env... Im pretty sure from my testing that the javascript execution just hung after the evalJSON (at least in IE) when we were trying that- we had to manually eval the responseText to get something we could iterate. i suppose using data would work, I''m guessing this is because [ ... ] is not valid json, yeah? If there''s no other way, i dont think the performance overhead is huge, more of a readability issue and remembering I have to use .data So to access this data, assuming it was returned in the body: $A(xhr.responseText.evalJSON().data).each(function (hashObj) { alert(hashObj); }); And via a header, which I think the key is x-json? (in ASP.NET, response.addHeader("x-json:{data:[{hash1...},{hash2...}]}") I think will put the data in an x-json header) then $A(xhr.responseJSON).each(function (hashObj) { alert(hashObj); }); I''ve not done much json, but I like the ability to get objects back instead of having to get text and parse it. Gareth On 6/9/07, Christophe Porteneuve <tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote:> > > Hey Gareth, howzit? :-) > > Gareth Evans a écrit : > > Could someone else identify the best way to get an array of hashes > > (which is returned as a string from an ajax call) and then get something > > you can iterate? > > So if I get you right, you''re saying that the following responseText > (just abbreviating with ''...'' in there): > > ''[ { hash1... }, { hash2... } ...]'' > > won''t get eval''d properly when you do xhr.responseText.evalJSON > (). Correct? > > What if you tried to wrap it in an extra object with a single property, > something like: > > ''{ data: [ { hash1... }, { hash2... } ...] }'' > > And then you''d access the data property of the eval''d object? Would > that work for you? I''m sorry I don''t have time to test this just now, > but I thought I''d suggest a possible lead. > > ''HTH > > -- > Christophe Porteneuve aka TDD > tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hey again, Er, I just tried Ajax.Request''ing a JSON array, with a simple target file that contains the following: [ { ''name'': ''Chris'', ''age'': 29 }, { ''name'': ''Sam'', ''age'': 23 } ] I do a xhr.responseText.evalJSON() and the result I get sure is an array of objects, I can perfectly well do: var obj = xhr.responseText.evalJSON(); console.log(obj[0].name); // ''Chris'' So you must be doing something weird with your JSON... -- Christophe Porteneuve aka TDD tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Must be, it was something I was doing a few weeks ago, my business partner was complaining the prototype code I wrote had started to die, just stopping when it got to the evalJSON. Marks question jogged my memory.. I think i''m going to be posting again in a sec- gotta implement code similar to igoogle with drag and drop zones and I''m not sure how to go about it... I''ll have a play and see how far I get.. On 6/9/07, Christophe Porteneuve <tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote:> > > Hey again, > > Er, I just tried Ajax.Request''ing a JSON array, with a simple target > file that contains the following: > > [ { ''name'': ''Chris'', ''age'': 29 }, { ''name'': ''Sam'', ''age'': 23 } ] > > I do a xhr.responseText.evalJSON() and the result I get sure is an array > of objects, I can perfectly well do: > > var obj = xhr.responseText.evalJSON(); > console.log(obj[0].name); // ''Chris'' > > So you must be doing something weird with your JSON... > > -- > Christophe Porteneuve aka TDD > tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hey, Gareth Evans a écrit :> $A(xhr.responseText.evalJSON().data).each(function (hashObj) { > alert(hashObj); });Just pass alert to each, it''ll be easier. But on arrays you''ll get sucky toString''s, so use inspect, which then warrants the use of an anonymous function. For instance: xhr.responseText.evalJSON().each(alert) // Sucky toString''s... xhr.responseText.evalJSON().each(function(o) { console.log($H(o).inspect()); // Better representations }); There''s no need for $A: what you get is already an Array, which is guaranteed extended no matter what browser you''re on. And the anonymous function here does not do anything extra to alert/console.log, so why bother wrapping the original call?> And via a header, which I think the key is x-json?Forget X-JSON :-) It''s only intended for on-the-side, small JSON data accompanying non-JSON results, such as XHTML fragments. If you''re looking for JSON as a main data format for your response, put it in the response''s body and set the Content-Type accordingly (application/json, IIRC), so you''ll be better off when 1.5.2 comes out :-)> response.addHeader("x-json:{data:[{hash1...},{hash2...}]}") I think willAlso, X-JSON is all uppercase.> I''ve not done much json, but I like the ability to get objects back > instead of having to get text and parse it.Totally. Screw XML and its daunting requirements for DOM traversal! -- Christophe Porteneuve aka TDD tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hey guys, Thanks for the great responses! The other day I ended up creating my associative array on the server side so that it instead of an array of objects, it output a JSON string that was of the format : ''{ { "VidID":1, "VidPathStr":"somepath", "VidDescrip":"somedescription" }, { "VidID":2, "VidPathStr":"Ima path", "VidDescrip":"blah blah" } , ...} '' ...this was different than what I had before which was indeed ''[ { hash1... }, { hash2... } ...]'' This way I was able to access the hashes as in the for loop below: function SearchVidSid2(SID) { var pars = ''FORM.Sid='' + parseInt(SID); var url = "/ss/Model/AjaxCalls/caller_getVidsBySid2.cfm" new Ajax.Request(url, { method: ''post'', parameters: pars, onComplete: function(transport) //get from JSON string { var jsonarray = transport.responseText.evalJSON(true); for (var i=0; i < Object.keys(jsonarray.VidID).size(); i++) { // for simplicity''s sake in demonstrating how I accessed the values in the hashes, showing alerts for each iteration alert(Object.values(jsonarray.VidID)[i]); alert(Object.values(jsonarray.VidPathStr)[i]); alert( Object.values(jsonarray.VidDescrip)[i]); } .... How does that look? (btw, in response to your message, in Seattle it''s 3:03 a.m.... time to go to bed (finally)! :) ...would like to hear your thoughts though! cheers, Mark On 6/9/07, Christophe Porteneuve <tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org > wrote:> > > Hey Gareth, howzit? :-) > > Gareth Evans a écrit : > > Could someone else identify the best way to get an array of hashes > > (which is returned as a string from an ajax call) and then get something > > you can iterate? > > So if I get you right, you''re saying that the following responseText > (just abbreviating with ''...'' in there): > > ''[ { hash1... }, { hash2... } ...]'' > > won''t get eval''d properly when you do xhr.responseText.evalJSON > (). Correct? > > What if you tried to wrap it in an extra object with a single property, > something like: > > ''{ data: [ { hash1... }, { hash2... } ...] }'' > > And then you''d access the data property of the eval''d object? Would > that work for you? I''m sorry I don''t have time to test this just now, > but I thought I''d suggest a possible lead. > > ''HTH > > -- > Christophe Porteneuve aka TDD > tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
...that was close to what I did... see my message (just sent).. I left the array out of it, and just grouped the Hashes as in an object... On 6/9/07, Christophe Porteneuve <tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote:> > > Hey Gareth, howzit? :-) > > Gareth Evans a écrit : > > Could someone else identify the best way to get an array of hashes > > (which is returned as a string from an ajax call) and then get something > > you can iterate? > > So if I get you right, you''re saying that the following responseText > (just abbreviating with ''...'' in there): > > ''[ { hash1... }, { hash2... } ...]'' > > won''t get eval''d properly when you do xhr.responseText.evalJSON > (). Correct? > > What if you tried to wrap it in an extra object with a single property, > something like: > > ''{ data: [ { hash1... }, { hash2... } ...] }'' > > And then you''d access the data property of the eval''d object? Would > that work for you? I''m sorry I don''t have time to test this just now, > but I thought I''d suggest a possible lead. > > ''HTH > > -- > Christophe Porteneuve aka TDD > tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hey, Mark Holton a écrit :> How does that look?Not too good: it''s not valid JSON, and perhaps not valid JS even: in an associative array, properties should have names; in your top-level container, the hashes are values with no names associated to them. So you should use an array as the top-level container, or change your structure so that each hash is associated to a name. -- Christophe Porteneuve aka TDD tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
...it was valid JS, but what I wrote was not the JSON string that was returned.... going by memory as to what the output of the JSON string was, and it''s 3:00 a.m. I think you are right that each hash had an ID next to it. ...hold on, I''ll get what the actual return string was... On 6/9/07, Christophe Porteneuve <tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote:> > > Hey, > > Mark Holton a écrit : > > How does that look? > > Not too good: it''s not valid JSON, and perhaps not valid JS even: in an > associative array, properties should have names; in your top-level > container, the hashes are values with no names associated to them. So > you should use an array as the top-level container, or change your > structure so that each hash is associated to a name. > > -- > Christophe Porteneuve aka TDD > tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Chris, Here is the actual JSON I was returning, and then using Object.values( jsonarray.VidID)[i], etc, etc to grab the values: {"VidPathStr":{"1":"anotherpath","2":"somepath"},"VidDescrip":{"1":"another description","2":"some description"},"VidID":{"1":3473,"2":3472}} Sorry for the confusion earlier. (which do you think is a better/ faster structure to access -- the array of hashes, or this JSON above?). Your thoughts? -Mark On 6/9/07, Mark Holton <holtonma-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hey guys, > Thanks for the great responses! > > The other day I ended up creating my associative array on the server side > so that it instead of an array of objects, it output a JSON string that was > of the format : > {"VidPathStr":{"1":"anotherpath","2":"somepath"},"VidDescrip":{"1":"another > description","2":"some description"},"VidID":{"1":3473,"2":3472}} > > ...this was different than what I had before which was indeed ''[ { > hash1... }, { hash2... } ...]'' > > This way I was able to access the hashes as in the for loop below: > > function SearchVidSid2(SID) { > var pars = ''FORM.Sid='' + parseInt(SID); > var url = "/ss/Model/AjaxCalls/caller_getVidsBySid2.cfm" > > new Ajax.Request(url, > { > method: ''post'', > parameters: pars, > onComplete: function(transport) //get from JSON string > { > var jsonarray = transport.responseText.evalJSON(true); > > for (var i=0; i < Object.keys(jsonarray.VidID).size(); > i++) { > // for simplicity''s sake in demonstrating how I > accessed the values in the hashes, showing alerts for each iteration > alert(Object.values(jsonarray.VidID)[i]); > alert(Object.values(jsonarray.VidPathStr)[i]); > alert( Object.values(jsonarray.VidDescrip)[i]); > > } > .... > > How does that look? > (btw, in response to your message, in Seattle it''s 3:03 a.m.... time to go > to bed (finally)! :) ...would like to hear your thoughts though! > cheers, > Mark > > On 6/9/07, Christophe Porteneuve <tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org > wrote: > > > > > > Hey Gareth, howzit? :-) > > > > Gareth Evans a écrit : > > > Could someone else identify the best way to get an array of hashes > > > (which is returned as a string from an ajax call) and then get > > something > > > you can iterate? > > > > So if I get you right, you''re saying that the following responseText > > (just abbreviating with ''...'' in there): > > > > ''[ { hash1... }, { hash2... } ...]'' > > > > won''t get eval''d properly when you do xhr.responseText.evalJSON > > (). Correct? > > > > What if you tried to wrap it in an extra object with a single property, > > something like: > > > > ''{ data: [ { hash1... }, { hash2... } ...] }'' > > > > And then you''d access the data property of the eval''d object? Would > > that work for you? I''m sorry I don''t have time to test this just now, > > but I thought I''d suggest a possible lead. > > > > ''HTH > > > > -- > > Christophe Porteneuve aka TDD > > tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hey Mark, Mark Holton a écrit :> Sorry for the confusion earlier. (which do you think is a better/ > faster structure to access -- the array of hashes, or this JSON > above?). Your thoughts?They''re probably equivalent from a performance standpoint. It all depends on what makes more sense from a business perspective: accessing your items by name or numerical index? If the former, use your current variant, if the latter, go for an array. -- Christophe Porteneuve aka TDD tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sounds reasonable, thanks ...it''s nice to have the two options when creating & accessing json. Appreciate the feedback gnite- On 6/9/07, Christophe Porteneuve <tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote:> > > Hey Mark, > > Mark Holton a écrit : > > Sorry for the confusion earlier. (which do you think is a better/ > > faster structure to access -- the array of hashes, or this JSON > > above?). Your thoughts? > > They''re probably equivalent from a performance standpoint. It all > depends on what makes more sense from a business perspective: accessing > your items by name or numerical index? If the former, use your current > variant, if the latter, go for an array. > > -- > Christophe Porteneuve aka TDD > tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---