SuZi
SuZi

Reputation: 19

Why does this JSON.parse code not work?

I am trying to pass json encoded values from a php script to a, GnuBookTest.js, javascript file that initiates a Bookreader object and use the values i have passed in via the variable i named "result".

The php script is sending the values like:

<div id="bookreader">
 <div id="BookReader" style="left:10px; right:10px; top:30px; bottom:30px;">x</div>
 <script type="text/javascript">var result = {"istack":"zi94sm65\/BUCY\/BUCY200707170530PM","leafCount":"14","wArr":"[893,893,893,893,893,893,893,893,893,893,893,893,893,893]","hArr":"[1155,1155,1155,1155,1155,1155,1155,1155,1155,1155,1155,1155,1155,1155]","leafArr":"[0,1,2,3,4,5,6,7,8,9,10,11,12,13]","sd":"[\"RIGHT\",\"LEFT\",\"RIGHT\",\"LEFT\",\"RIGHT\",\"LEFT\",\"RIGHT\",\"LEFT\",\"RIGHT\",\"LEFT\",\"RIGHT\",\"LEFT\",\"RIGHT\",\"LEFT\"]"}</script>
 <script type="text/javascript" src="http://localhost:8080/application/js/GnuBookTest.js"></script>
 </div>
</div>

and in the GnuBookTest.js file i am trying to use the values like:

br = new BookReader();

// Return the width of a given page.
br.getPageWidth = function(index) {
     return this.pageW[index];
}

// Return the height of a given page.
br.getPageHeight = function(index) {
    return this.pageH[index];
}

br.pageW = JSON.parse(result.wArr);

br.pageH = JSON.parse(result.hArr);

br.leafMap = JSON.parse(result.leafArr);

//istack is an url fragment for location of image files
var istack = result.istack;
.
.
.

Using JSON.parse as i have written it above loads the Bookreader and uses my values correctly in a few web-browsers: Firefox, IE8, and desktop-Safari; but does not work at all in mac-Chrome, mobile-Safari, plus older versions of IE. Mobile safari keeps giving me a reference error msg: can't find variable: JSON. The other browsers just do not load the Bookreader and show the "x" instead, like they did not get the values from the php script.

Where is the problem?

Upvotes: 1

Views: 2749

Answers (2)

Jeff Beaman
Jeff Beaman

Reputation: 2941

Older browsers do support JSON, they just don't support JSON.parse. To load JSON data in older browsers you can simply use

var obj = eval('('+jsonStr + ')');

Upvotes: 1

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107586

Older browsers don't have native JSON support. You'll likely have to include it manually.

Upvotes: 5

Related Questions