Hey Guys I have an ajax method that returns the following JSON. {''ProductId'':''18'', ''Data'':[{''Id'':''1'',''Name'':''In Stock'',''Colour'':''#000000'' }, {''Id'':''2'',''Name'':''Ordered'',''Colour'':''#DE8300'' }, {''Id'':''3'',''Name'':''Backordered'',''Colour'':''#E20000'' }]} As you can see, it has a ProductId, and then an array of objects which make up a list of available statuses. When I try to convert it into an object, so I can go obj.ProductId and obj.Data.each(function(item) { ... }); the javascript engine (i''m currently working in IE) just stops executing. Any alerts etc after the line where I try to eval the json never happen, and the debugger is not entered. I''m working from VWD Express and when I make script typos, it throws into the debugger as soon as something goes wrong. from my onSuccess handler function processSuccessfulRequest (transport) { } I''ve tried: var obj = eval(transport.responseText); and var obj = transport.responseText.evalJSON(true); Can anyone provide any insight into how to get this working? I often need to return arrays of objects from server side pages, and in this case, I''ve got the other bit of data ProductId which I will also need. I have to bring ProductId back because it is available in the event handler which started the request, but once the asyncronous request goes out, i''ve lost the context of the event so I need that data to get it back again. 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 -~----------~----~----~----~------~----~------~--~---
Strange, this should work! A couple of suggestions: 1. make sure your JSON is valid (double quotes instead of single quotes for example), 2. use ''....''.evalJSON() (and not evalJSON(true), thus bypassing the regexp), 3. upgrade to Prototype 1.5.1.1, 4. try evaluating the json data directly (i.e. without the ajax call, to make sure that''s where the problem lies) ... 5. try removing any extra whitespaces, linebreaks, etc. Hope this helps, Regards, Tobie On Jul 19, 2:56 am, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey Guys > > I have an ajax method that returns the following JSON. > > {''ProductId'':''18'', ''Data'':[{''Id'':''1'',''Name'':''In Stock'',''Colour'':''#000000'' }, > {''Id'':''2'',''Name'':''Ordered'',''Colour'':''#DE8300'' }, > {''Id'':''3'',''Name'':''Backordered'',''Colour'':''#E20000'' }]} > > As you can see, it has a ProductId, and then an array of objects which make > up a list of available statuses. > > When I try to convert it into an object, so I can go > obj.ProductId and obj.Data.each(function(item) { ... }); the javascript > engine (i''m currently working in IE) just stops executing. > Any alerts etc after the line where I try to eval the json never happen, and > the debugger is not entered. > I''m working from VWD Express and when I make script typos, it throws into > the debugger as soon as something goes wrong. > > from my onSuccess handler > > function processSuccessfulRequest (transport) { > > } > > I''ve tried: > var obj = eval(transport.responseText); > and > var obj = transport.responseText.evalJSON(true); > > Can anyone provide any insight into how to get this working? I often need to > return arrays of objects from server side pages, and in this case, I''ve got > the other bit of data ProductId which I will also need. > I have to bring ProductId back because it is available in the event handler > which started the request, but once the asyncronous request goes out, i''ve > lost the context of the event so I need that data to get it back again. > > 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 -~----------~----~----~----~------~----~------~--~---
Hi Tobie, and list. Yeah thats what I thought mate, but it wasn''t. Your suggestions were most helpful, 1. Changed to using double quotes instead of single across the entire JSON. This worked. {"ProductId":"18", "Data":[{"Id":"1","Name":"In Stock","Colour":"#000000" }, {"Id":''2'',"Name":"Ordered","Colour":"#DE8300" }, {Id:"3",Name:"Backordered",Colour:"#E20000" }]} 2. Didn''t need to try evalJSON once the double quotes worked but changed back to single quotes for the benefit of figuring out whats going wrong. {ProductId:''18'', Data:[{Id:''1'',Name:''In Stock'',Colour:''#000000'' }, {Id:''2'',Name:''Ordered'',Colour:''#DE8300'' }, {Id:''3'',Name:''Backordered'',Colour:''#E20000'' }]} and {''ProductId'':''18'', Data:[{''Id'':''1'',''Name'':''In Stock'',''Colour'':''#000000'' }, {''Id'':''2'',''Name'':''Ordered'',''Colour'':''#DE8300'' }, {''Id'':''3'',''Name'':''Backordered'',''Colour'':''#E20000'' }]} Both fail with evalJSON(true) and pass with evalJSON() 3. Already running 1.5.1.1 4. I think the JSON data is the cause as proved in the earlier steps. This is probably redundant now. 5. No linebreaks in the actual JSON and the only whitespace is immediately following any commas or before } (so I can use CTRL->Arrow to jump around the json. So, it seems that indeed the cause of the problem is the single quotes. The reason I used single quotes to begin with is when the JSON string is built on the server side, the string delimiter is " so you need to escape it (in VB this is "" (ie two double quotes)) This gets messy *really* quickly when you start getting nested levels of quotes, or need to open a string, and include a quote and close it again: ie: someVar & """,""Data"":""" & someOtherVar etc... So, just to confirm, single quotes in JSON is invalid? I can see this as a problem that will be encountered by others in time, hence the detailed reply. Gareth On 7/19/07, Tobie Langel <tobie.langel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Strange, this should work! > > A couple of suggestions: > 1. make sure your JSON is valid (double quotes instead of single > quotes for example), > 2. use ''....''.evalJSON() (and not evalJSON(true), thus bypassing the > regexp), > 3. upgrade to Prototype 1.5.1.1, > 4. try evaluating the json data directly (i.e. without the ajax call, > to make sure that''s where the problem lies) ... > 5. try removing any extra whitespaces, linebreaks, etc. > > Hope this helps, > > Regards, > > Tobie > > On Jul 19, 2:56 am, "Gareth Evans" <agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hey Guys > > > > I have an ajax method that returns the following JSON. > > > > {''ProductId'':''18'', ''Data'':[{''Id'':''1'',''Name'':''In > Stock'',''Colour'':''#000000'' }, > > {''Id'':''2'',''Name'':''Ordered'',''Colour'':''#DE8300'' }, > > {''Id'':''3'',''Name'':''Backordered'',''Colour'':''#E20000'' }]} > > > > As you can see, it has a ProductId, and then an array of objects which > make > > up a list of available statuses. > > > > When I try to convert it into an object, so I can go > > obj.ProductId and obj.Data.each(function(item) { ... }); the javascript > > engine (i''m currently working in IE) just stops executing. > > Any alerts etc after the line where I try to eval the json never happen, > and > > the debugger is not entered. > > I''m working from VWD Express and when I make script typos, it throws > into > > the debugger as soon as something goes wrong. > > > > from my onSuccess handler > > > > function processSuccessfulRequest (transport) { > > > > } > > > > I''ve tried: > > var obj = eval(transport.responseText); > > and > > var obj = transport.responseText.evalJSON(true); > > > > Can anyone provide any insight into how to get this working? I often > need to > > return arrays of objects from server side pages, and in this case, I''ve > got > > the other bit of data ProductId which I will also need. > > I have to bring ProductId back because it is available in the event > handler > > which started the request, but once the asyncronous request goes out, > i''ve > > lost the context of the event so I need that data to get it back again. > > > > 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 -~----------~----~----~----~------~----~------~--~---
The JSON standard is explained here: http://www.json.org/, and yep it actually does specify double quotes (although I''ve no real good idea why the restriction since there is also the provision for the "\" control character for escaping). On 7/19/07, Gareth Evans <agrath-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi Tobie, and list. > > Yeah thats what I thought mate, but it wasn''t. > > Your suggestions were most helpful, > > 1. Changed to using double quotes instead of single across the entire > JSON. This worked. > {"ProductId":"18", "Data":[{"Id":"1","Name":"In Stock","Colour":"#000000" > }, {"Id":''2'',"Name":"Ordered","Colour":"#DE8300" }, > {Id:"3",Name:"Backordered",Colour:"#E20000" }]} > > > 2. Didn''t need to try evalJSON once the double quotes worked but changed > back to single quotes for the benefit of figuring out whats going wrong. > > {ProductId:''18'', Data:[{Id:''1'',Name:''In Stock'',Colour:''#000000'' }, > {Id:''2'',Name:''Ordered'',Colour:''#DE8300'' }, > {Id:''3'',Name:''Backordered'',Colour:''#E20000'' }]} > and > {''ProductId'':''18'', Data:[{''Id'':''1'',''Name'':''In Stock'',''Colour'':''#000000'' }, > {''Id'':''2'',''Name'':''Ordered'',''Colour'':''#DE8300'' }, > {''Id'':''3'',''Name'':''Backordered'',''Colour'':''#E20000'' }]} > > Both fail with evalJSON(true) and pass with evalJSON() > > > 3. Already running 1.5.1.1 > > 4. I think the JSON data is the cause as proved in the earlier steps. This > is probably redundant now. > > 5. No linebreaks in the actual JSON and the only whitespace is immediately > following any commas or before } (so I can use CTRL->Arrow to jump around > the json. > > > So, it seems that indeed the cause of the problem is the single quotes. > > The reason I used single quotes to begin with is when the JSON string is > built on the server side, the string delimiter is " so you need to escape it > (in VB this is "" (ie two double quotes)) > This gets messy *really* quickly when you start getting nested levels of > quotes, or need to open a string, and include a quote and close it again: > ie: > > someVar & """,""Data"":""" & someOtherVar > etc... > > So, just to confirm, single quotes in JSON is invalid? > I can see this as a problem that will be encountered by others in time, > hence the detailed reply. > > Gareth > > On 7/19/07, Tobie Langel <tobie.langel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Strange, this should work! > > > > A couple of suggestions: > > 1. make sure your JSON is valid (double quotes instead of single > > quotes for example), > > 2. use ''....''.evalJSON() (and not evalJSON(true), thus bypassing the > > regexp), > > 3. upgrade to Prototype 1.5.1.1, > > 4. try evaluating the json data directly ( i.e. without the ajax call, > > to make sure that''s where the problem lies) ... > > 5. try removing any extra whitespaces, linebreaks, etc. > > > > Hope this helps, > > > > Regards, > > > > Tobie > > > > On Jul 19, 2:56 am, "Gareth Evans" < agr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hey Guys > > > > > > I have an ajax method that returns the following JSON. > > > > > > {''ProductId'':''18'', ''Data'':[{''Id'':''1'',''Name'':''In > > Stock'',''Colour'':''#000000'' }, > > > {''Id'':''2'',''Name'':''Ordered'',''Colour'':''#DE8300'' }, > > > {''Id'':''3'',''Name'':''Backordered'',''Colour'':''#E20000'' }]} > > > > > > As you can see, it has a ProductId, and then an array of objects which > > make > > > up a list of available statuses. > > > > > > When I try to convert it into an object, so I can go > > > obj.ProductId and obj.Data.each(function(item) { ... }); the > > javascript > > > engine (i''m currently working in IE) just stops executing. > > > Any alerts etc after the line where I try to eval the json never > > happen, and > > > the debugger is not entered. > > > I''m working from VWD Express and when I make script typos, it throws > > into > > > the debugger as soon as something goes wrong. > > > > > > from my onSuccess handler > > > > > > function processSuccessfulRequest (transport) { > > > > > > } > > > > > > I''ve tried: > > > var obj = eval(transport.responseText); > > > and > > > var obj = transport.responseText.evalJSON(true); > > > > > > Can anyone provide any insight into how to get this working? I often > > need to > > > return arrays of objects from server side pages, and in this case, > > I''ve got > > > the other bit of data ProductId which I will also need. > > > I have to bring ProductId back because it is available in the event > > handler > > > which started the request, but once the asyncronous request goes out, > > i''ve > > > lost the context of the event so I need that data to get it back > > again. > > > > > > Gareth > > > > > > > > > >-- Ryan Gahl Manager, Senior Software Engineer Nth Penguin, LLC http://www.nthpenguin.com -- Architect WebWidgetry.com / MashupStudio.com Future Home of the World''s First Complete Web Platform -- Inquire: 1-262-951-6727 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 -~----------~----~----~----~------~----~------~--~---
Yes, single quotes are invalid in JSON Happy to hear that this solved it. Regards, Tobie --~--~---------~--~----~------------~-------~--~----~ 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 20 Jul., 00:00, "Ryan Gahl" <ryan.g...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The JSON standard is explained here:http://www.json.org/, and yep it > actually does specify double quotes (although I''ve no real good idea why the > restriction since there is also the provision for the "\" control character > for escaping).The reason for specifying double quotes is that JSON is meant to be language independent. From json.org: "JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language." While single and double quotes are interchangeable in JavaScript, they are not in many other languages. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ah yes, that makes sense now when I think of it. On 7/20/07, Tobias <tobias.michaelsen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 20 Jul., 00:00, "Ryan Gahl" <ryan.g...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > The JSON standard is explained here:http://www.json.org/, and yep it > > actually does specify double quotes (although I''ve no real good idea why > the > > restriction since there is also the provision for the "\" control > character > > for escaping). > > The reason for specifying double quotes is that JSON is meant to be > language independent. From json.org: "JSON is a text format that is > completely language independent but uses conventions that are familiar > to programmers of the C-family of languages, including C, C++, C#, > Java, JavaScript, Perl, Python, and many others. These properties make > JSON an ideal data-interchange language." > > While single and double quotes are interchangeable in JavaScript, they > are not in many other languages. > > > > >-- Ryan Gahl Manager, Senior Software Engineer Nth Penguin, LLC http://www.nthpenguin.com -- Architect WebWidgetry.com / MashupStudio.com Future Home of the World''s First Complete Web Platform -- Inquire: 1-262-951-6727 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 -~----------~----~----~----~------~----~------~--~---