Hey Guys Re: Nested hashes/arrays of hashes I know Colin was talking about this some time back but I just wanted to clarify whether these actually work... I had a scenario where I wanted to return effectively an array of hashes (though nested hashes would be better) for metadata about form fields (to implement a cascading requirement) eg: Client sends "Field 1 changed to 2" Server replies with {fieldid:''fielda'',properties:{required:true,label:''Field A''}},{fieldid:''fieldb'',properties:{required:true,label:''Field B''}} (which in this case might be dependent on the fact that field1 has changed to 2, rather than 3 or 4) Client receives reply and loads the data and iterates through the received meta setting the various properties as appropriate. However, I wasn''t sure what the format of the reply should be in order to implement such functionality. It doesn''t need the nested hashes but I do need to return an array of hashes. I think I returned a 1 dimensional ~ seperated array and then split it to iterate but I''m sure there''s a better way. Could someone give me an example of the reply and the method to get it into a hash, if not just $H(transport.responseText) I tried the $H method as well as the toJSON (or is it fromJSON, i cant remember) Gareth --~--~---------~--~----~------------~-------~--~----~ 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 Colin, thats what I thought. I''m using ASP.Net, are you able to provide an example of an array of hashes in JSON? I don''t hve a json_encode function so not sure what the output should be. If it''s not too much trouble. I''ve looked at the json info on prototypejs.org but not much there, and the json homepage didn''t have a lot of examples from memory. It''s not urgent, more for my own knowledge for next time. Gareth On 4/3/07, Colin Mollenhour <eliteii92g-NPSFNn/7+NYVo650/ln6uw@public.gmane.org> wrote:> > > Prototype Core team decided not to support encoding/decoding of nested > arrays/hashes in parameter strings for various reasons, so JSON is going > to be the way to go with this (and is better anyway). I don''t know what > server-side platform you use, but in PHP a simple json_encode(<your data > structure here>) will convert any array/hash (including nesting) into a > JSON string. I recommend sending it through the body of the response, > not the X-JSON header because of header size limitations. So, on the > client-side, you can do this to get basically the exact same data > structure that you had on the server side into javascript: > > var obj = transport.responseText.evalJSON(); > > So if what you passed to json_encode (or your equivalent) on the server > side was an array of hashes, "obj" would now be the same array of hashes > you had before and you can use Prototype Enumerables on it as you please. > > Colin > > Gareth Evans wrote: > > Hey Guys > > > > Re: Nested hashes/arrays of hashes > > > > I know Colin was talking about this some time back but I just wanted > > to clarify whether these actually work... > > I had a scenario where I wanted to return effectively an array of > > hashes (though nested hashes would be better) for metadata about form > > fields (to implement a cascading requirement) > > > > eg: > > Client sends "Field 1 changed to 2" > > Server replies with > > {fieldid:''fielda'',properties:{required:true,label:''Field > > A''}},{fieldid:''fieldb'',properties:{required:true,label:''Field B''}} > > (which in this case might be dependent on the fact that field1 has > > changed to 2, rather than 3 or 4) > > Client receives reply and loads the data and iterates through the > > received meta setting the various properties as appropriate. > > > > However, I wasn''t sure what the format of the reply should be in order > > to implement such functionality. > > It doesn''t need the nested hashes but I do need to return an array of > > hashes. > > I think I returned a 1 dimensional ~ seperated array and then split it > > to iterate but I''m sure there''s a better way. > > > > Could someone give me an example of the reply and the method to get it > > into a hash, if not just $H(transport.responseText) > > I tried the $H method as well as the toJSON (or is it fromJSON, i cant > > remember) > > > > Gareth > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Gareth, the Newtonsoft Colin linked to is the one I use as well. It''s pretty good. On 4/2/07, Colin Mollenhour <eliteii92g-NPSFNn/7+NYVo650/ln6uw@public.gmane.org> wrote:> > Found this link on json.org > http://www.newtonsoft.com/products/json/ (example included) > I''m not an ASP.Net programmer but that looks like what you need. If not, > look into Jayrock (also a link from json.org) > > Colin > > Gareth Evans wrote: > > Thanks Colin, thats what I thought. > > I''m using ASP.Net, are you able to provide an example of an array of > hashes in JSON? I don''t hve a json_encode function so not sure what the > output should be. > If it''s not too much trouble. > I''ve looked at the json info on prototypejs.org but not much there, and > the json homepage didn''t have a lot of examples from memory. > > It''s not urgent, more for my own knowledge for next time. > > Gareth > > > On 4/3/07, Colin Mollenhour <eliteii92g-NPSFNn/7+NYVo650/ln6uw@public.gmane.org> wrote: > > > > > > Prototype Core team decided not to support encoding/decoding of nested > > arrays/hashes in parameter strings for various reasons, so JSON is going > > > > to be the way to go with this (and is better anyway). I don''t know what > > server-side platform you use, but in PHP a simple json_encode(<your data > > structure here>) will convert any array/hash (including nesting) into a > > JSON string. I recommend sending it through the body of the response, > > not the X-JSON header because of header size limitations. So, on the > > client-side, you can do this to get basically the exact same data > > structure that you had on the server side into javascript: > > > > var obj = transport.responseText.evalJSON(); > > > > So if what you passed to json_encode (or your equivalent) on the server > > side was an array of hashes, "obj" would now be the same array of hashes > > you had before and you can use Prototype Enumerables on it as you > > please. > > > > Colin > > > > Gareth Evans wrote: > > > Hey Guys > > > > > > Re: Nested hashes/arrays of hashes > > > > > > I know Colin was talking about this some time back but I just wanted > > > to clarify whether these actually work... > > > I had a scenario where I wanted to return effectively an array of > > > hashes (though nested hashes would be better) for metadata about form > > > fields (to implement a cascading requirement) > > > > > > eg: > > > Client sends "Field 1 changed to 2" > > > Server replies with > > > {fieldid:''fielda'',properties:{required:true,label:''Field > > > A''}},{fieldid:''fieldb'',properties:{required:true,label:''Field B''}} > > > (which in this case might be dependent on the fact that field1 has > > > changed to 2, rather than 3 or 4) > > > Client receives reply and loads the data and iterates through the > > > received meta setting the various properties as appropriate. > > > > > > However, I wasn''t sure what the format of the reply should be in order > > > to implement such functionality. > > > It doesn''t need the nested hashes but I do need to return an array of > > > hashes. > > > I think I returned a 1 dimensional ~ seperated array and then split it > > > to iterate but I''m sure there''s a better way. > > > > > > Could someone give me an example of the reply and the method to get it > > > > > into a hash, if not just $H(transport.responseText) > > > I tried the $H method as well as the toJSON (or is it fromJSON, i cant > > > remember) > > > > > > Gareth > > > > > > > > > > > > > > > > > > > >-- Ryan Gahl Application Development Consultant Athena Group, Inc. Inquire: 1-920-955-1457 Blog: http://www.someElement.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Prototype Core team decided not to support encoding/decoding of nested arrays/hashes in parameter strings for various reasons, so JSON is going to be the way to go with this (and is better anyway). I don''t know what server-side platform you use, but in PHP a simple json_encode(<your data structure here>) will convert any array/hash (including nesting) into a JSON string. I recommend sending it through the body of the response, not the X-JSON header because of header size limitations. So, on the client-side, you can do this to get basically the exact same data structure that you had on the server side into javascript: var obj = transport.responseText.evalJSON(); So if what you passed to json_encode (or your equivalent) on the server side was an array of hashes, "obj" would now be the same array of hashes you had before and you can use Prototype Enumerables on it as you please. Colin Gareth Evans wrote:> Hey Guys > > Re: Nested hashes/arrays of hashes > > I know Colin was talking about this some time back but I just wanted > to clarify whether these actually work... > I had a scenario where I wanted to return effectively an array of > hashes (though nested hashes would be better) for metadata about form > fields (to implement a cascading requirement) > > eg: > Client sends "Field 1 changed to 2" > Server replies with > {fieldid:''fielda'',properties:{required:true,label:''Field > A''}},{fieldid:''fieldb'',properties:{required:true,label:''Field B''}} > (which in this case might be dependent on the fact that field1 has > changed to 2, rather than 3 or 4) > Client receives reply and loads the data and iterates through the > received meta setting the various properties as appropriate. > > However, I wasn''t sure what the format of the reply should be in order > to implement such functionality. > It doesn''t need the nested hashes but I do need to return an array of > hashes. > I think I returned a 1 dimensional ~ seperated array and then split it > to iterate but I''m sure there''s a better way. > > Could someone give me an example of the reply and the method to get it > into a hash, if not just $H(transport.responseText) > I tried the $H method as well as the toJSON (or is it fromJSON, i cant > remember) > > Gareth > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"> </head> <body bgcolor="#ffffff" text="#000000"> Found this link on json.org<br> <a class="moz-txt-link-freetext" href="http://www.newtonsoft.com/products/json/">http://www.newtonsoft.com/products/json/</a> (example included)<br> I''m not an ASP.Net programmer but that looks like what you need. If not, look into Jayrock (also a link from json.org)<br> <br> Colin<br> <br> Gareth Evans wrote: <blockquote cite="mid:46944cba0704021845m1d5fe65fxf3733f0caeb862f2-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org" type="cite"> <div>Thanks Colin, thats what I thought.</div> <div> </div> <div>I''m using ASP.Net, are you able to provide an example of an array of hashes in JSON? I don''t hve a json_encode function so not sure what the output should be.</div> <div>If it''s not too much trouble.</div> <div>I''ve looked at the json info on <a moz-do-not-send="true" href="http://prototypejs.org">prototypejs.org</a> but not much there, and the json homepage didn''t have a lot of examples from memory.</div> <div> </div> <div>It''s not urgent, more for my own knowledge for next time.</div> <div> </div> <div>Gareth<br> <br> </div> <div><span class="gmail_quote">On 4/3/07, <b class="gmail_sendername">Colin Mollenhour</b> <<a moz-do-not-send="true" href="mailto:eliteii92g-NPSFNn/7+NYVo650/ln6uw@public.gmane.org">eliteii92g-NPSFNn/7+NYVo650/ln6uw@public.gmane.org</a>> wrote:</span> <blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0px 0px 0px 0.8ex; padding-left: 1ex;"><br> Prototype Core team decided not to support encoding/decoding of nested<br> arrays/hashes in parameter strings for various reasons, so JSON is going <br> to be the way to go with this (and is better anyway). I don''t know what<br> server-side platform you use, but in PHP a simple json_encode(<your data<br> structure here>) will convert any array/hash (including nesting) into a <br> JSON string. I recommend sending it through the body of the response,<br> not the X-JSON header because of header size limitations. So, on the<br> client-side, you can do this to get basically the exact same data<br> structure that you had on the server side into javascript: <br> <br> var obj = transport.responseText.evalJSON();<br> <br> So if what you passed to json_encode (or your equivalent) on the server<br> side was an array of hashes, "obj" would now be the same array of hashes<br> you had before and you can use Prototype Enumerables on it as you please.<br> <br> Colin<br> <br> Gareth Evans wrote:<br> > Hey Guys<br> ><br> > Re: Nested hashes/arrays of hashes<br> ><br> > I know Colin was talking about this some time back but I just wanted <br> > to clarify whether these actually work...<br> > I had a scenario where I wanted to return effectively an array of<br> > hashes (though nested hashes would be better) for metadata about form<br> > fields (to implement a cascading requirement) <br> ><br> > eg:<br> > Client sends "Field 1 changed to 2"<br> > Server replies with<br> > {fieldid:''fielda'',properties:{required:true,label:''Field<br> > A''}},{fieldid:''fieldb'',properties:{required:true,label:''Field B''}} <br> > (which in this case might be dependent on the fact that field1 has<br> > changed to 2, rather than 3 or 4)<br> > Client receives reply and loads the data and iterates through the<br> > received meta setting the various properties as appropriate. <br> ><br> > However, I wasn''t sure what the format of the reply should be in order<br> > to implement such functionality.<br> > It doesn''t need the nested hashes but I do need to return an array of<br> > hashes. <br> > I think I returned a 1 dimensional ~ seperated array and then split it<br> > to iterate but I''m sure there''s a better way.<br> ><br> > Could someone give me an example of the reply and the method to get it <br> > into a hash, if not just $H(transport.responseText)<br> > I tried the $H method as well as the toJSON (or is it fromJSON, i cant<br> > remember)<br> ><br> > Gareth<br> ><br> > ><br> <br> <br> <br> <br> </blockquote> </div> </blockquote> <br> --~--~---------~--~----~------------~-------~--~----~<br> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. <br> To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <br> To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <br> For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en <br> -~----------~----~----~----~------~----~------~--~---<br> </body> </html> <br>