JSON object not parsing in firefox
|
|
Thread rating:  |
saril.ks@gmail.com - 27 Sep 2007 18:57 GMT Hi The JSON object I am using is as below . this object is returned after an AJAX call
{"application" :[ {optionValue:"101", optionDisplay: "estmt"}, {optionValue:"11", optionDisplay: "Arif"}, {optionValue:"12", optionDisplay: "JC"} ]}
In the JS i am using the below code
data = ajaxRequest.responseText ; var jsonOBJ = eval('(' + data + ')'); len = jsonOBJ.application.length ; key = jsonOBJ.application[i].optionValue ; value = jsonOBJ.application[i].optionDisplay ;
Internet Explorer is able to process the above code , but while trying from Firefox , I am getting the below error message
jsonOBJ has no properties [Break on this error] len = jsonOBJ.application.length ;
Please help
Thanks S
David Mark - 27 Sep 2007 19:11 GMT On Sep 27, 1:57 pm, saril...@gmail.com wrote:
> Hi > The JSON object I am using is as below . this object is returned after [quoted text clipped - 24 lines] > Thanks > S I couldn't break Firefox with this:
var data = '{"application" :[\n\r{optionValue:"101", optionDisplay: "estmt"},\n\r {optionValue:"11", optionDisplay: "Arif"},\n\r {optionValue:"12", optionDisplay: "JC"}\n\r ]}'; var jsonOBJ = eval('(' + data + ')'); len = jsonOBJ.application.length; alert(len);
Are you sure about the responseText value?
saril.ks@gmail.com - 27 Sep 2007 19:31 GMT > On Sep 27, 1:57 pm, saril...@gmail.com wrote: > [quoted text clipped - 39 lines] > > - Show quoted text - Hi Thanks for the reply . I am geting the responseText value as below
{"application" :[ {optionValue:"101", optionDisplay: "estmt"}, {optionValue:"11", optionDisplay: "Arif" },{optionValue:"12", optionDisplay: "JC"}]}
jsonOBJ has no properties [Break on this error] len = jsonOBJ.application.length ;
appreciate your help
David Mark - 27 Sep 2007 19:36 GMT On Sep 27, 2:31 pm, saril...@gmail.com wrote:
> > On Sep 27, 1:57 pm, saril...@gmail.com wrote: > [quoted text clipped - 52 lines] > > appreciate your help So try what I did. Does it work for you?
Thomas 'PointedEars' Lahn - 27 Sep 2007 20:13 GMT >> I couldn't break Firefox with this: >> [quoted text clipped - 15 lines] > {optionValue:"11", optionDisplay: "Arif" > },{optionValue:"12", optionDisplay: "JC"}]} Evaluating that results in a compile error:
| Error: invalid label Tested in Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7 (with Firebug 1.05).
In contrast,
[ {optionValue:"101", optionDisplay: "estmt"}, {optionValue:"11", optionDisplay: "Arif"},{optionValue:"12", optionDisplay: "JC"}]
evaluates fine.
> jsonOBJ has no properties > [Break on this error] len = jsonOBJ.application.length ; Because jsonOBJ would have the value `undefined' if the evaluation failed, if that.
PointedEars
 Signature Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee
Thomas 'PointedEars' Lahn - 27 Sep 2007 20:17 GMT >> [...] I am geting the responseText value as below >> [quoted text clipped - 15 lines] > > evaluates fine. {application: [ {optionValue:"102", optionDisplay: "estmt"}, {optionValue:"11", optionDisplay: "Arif"},{optionValue:"12", optionDisplay: "JC"}]}
is OK, too.
PointedEars
 Signature Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee
Thomas 'PointedEars' Lahn - 27 Sep 2007 20:26 GMT >>> [...] I am geting the responseText value as below >>> [quoted text clipped - 20 lines] > > is OK, too. I presume the reason why it breaks with `"application":' is that the outer `{...}' is regarded as delimiters of a block statement by eval(), and not as delimiters of the Object literal notation. Hence eval()uating the variant with `application:' yields only the Array object, which has no `application' property. Both `"application":' and `application:' are merely regarded as labels in a block statement there, with the former being invalid.
PointedEars
 Signature var bugRiddenCrashPronePieceOfJunk = ( navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1 ) // Plone, register_function.js:16
saril.ks@gmail.com - 27 Sep 2007 22:21 GMT On Sep 27, 12:26 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de> wrote:
> >> saril...@gmail.com wrote: > >>> [...] I am geting the responseText value as below [quoted text clipped - 37 lines] > > - Show quoted text - Hi I got the solution . tried without quotes and it worked fine . Thanks to all for the help
S
Thomas 'PointedEars' Lahn - 27 Sep 2007 22:45 GMT >> I presume the reason why it breaks with `"application":' is that the outer >> `{...}' is regarded as delimiters of a block statement by eval(), and not as [quoted text clipped - 3 lines] >> labels in a block statement there, with the former being invalid. >> [...] Please trim your quotes. http://jibbering.com/faq/
> Hi > I got the solution . tried without quotes and it worked fine . Thanks > to all for the help I don't think you have relized yet that you are shooting yourself in the foot. As I have pointed out, because of the block statement - object literal and property name - label ambiguities, `jsonOBJ.application.length' may or may not work with unquoted `application', depending on the script engine that compiles this. You should use JSON.parse() instead.
PointedEars
 Signature var bugRiddenCrashPronePieceOfJunk = ( navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1 ) // Plone, register_function.js:16
saril.ks@gmail.com - 27 Sep 2007 22:53 GMT On Sep 27, 2:45 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de> wrote:
> saril...@gmail.com wrote: > >> I presume the reason why it breaks with `"application":' is that the outer [quoted text clipped - 23 lines] > && navigator.userAgent.indexOf('Mac') != -1 > ) // Plone, register_function.js:16 I did not use JSON.parse() as it was throwing exceptions in firefox . But when i unquoted the 'application' , i am able to parse the jsonOBJ . Do you have any snippets to handle JSON.parse() exception ?
Thanks S
Thomas 'PointedEars' Lahn - 29 Sep 2007 14:16 GMT > I did not use JSON.parse() as it was throwing exceptions in firefox . > But when i unquoted the 'application' , i am able to parse the > jsonOBJ . Do you have any snippets to handle JSON.parse() > exception ? try { ajaxRequest.responseText.parseJSON() } catch (e) { // ... }
PointedEars
 Signature realism: HTML 4.01 Strict evangelism: XHTML 1.0 Strict madness: XHTML 1.1 as application/xhtml+xml -- Bjoern Hoehrmann
Randy Webb - 29 Sep 2007 21:09 GMT Thomas 'PointedEars' Lahn said the following on 9/29/2007 9:16 AM:
>> I did not use JSON.parse() as it was throwing exceptions in firefox . >> But when i unquoted the 'application' , i am able to parse the >> jsonOBJ . Do you have any snippets to handle JSON.parse() >> exception ? > > try Error prone on the web.
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Thomas 'PointedEars' Lahn - 30 Sep 2007 10:02 GMT > Thomas 'PointedEars' Lahn said the following on 9/29/2007 9:16 AM: >>> I did not use JSON.parse() as it was throwing exceptions in firefox . ^^^^^^^^^^^^^^^^^^^
>>> But when i unquoted the 'application' , i am able to parse the >>> jsonOBJ . Do you have any snippets to handle JSON.parse() >>> exception ? >> try > > Error prone on the web. Because of NN/IE 4.x?
It adds no more error-proneness here because -- surprise, surprise! -- the current JSON parser implementation in J(ava)Script already uses `throw' to throw the aforementioned exception:
http://www.json.org/js.html
The incompatibility with try...catch in ECMAScript < 3 implementations can be worked around proprietarily with window.onerror and eval(), though.
PointedEars
 Signature var bugRiddenCrashPronePieceOfJunk = ( navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1 ) // Plone, register_function.js:16
Randy Webb - 30 Sep 2007 11:21 GMT Thomas 'PointedEars' Lahn said the following on 9/30/2007 5:02 AM:
>> Thomas 'PointedEars' Lahn said the following on 9/29/2007 9:16 AM: >>>> I did not use JSON.parse() as it was throwing exceptions in firefox . [quoted text clipped - 6 lines] > > Because of NN/IE 4.x? Are you saying that you believe that NN4.x and IE4.x are the only browsers potentially being used that don't support try/catch? try/catch is error prone on the web.
> It adds no more error-proneness here because -- surprise, surprise! -- the > current JSON parser implementation in J(ava)Script already uses `throw' to > throw the aforementioned exception: That doesn't make try/catch/throw any less error prone.
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
|
|
|