简体   繁体   中英

JSON.parse not working on older mobile browsers

I have a JSON string A. A is in the format:

'[{"key1":"val1",...,"keyn":"valn"},{...},...,{...}]'

A is created on a php server using json_encode(Array).

var str = JSON.parse(A); works in all desktop browsers, mobile safari, mobile chrome etc..

var str = JSON.parse(A); throws an exception('exception: cannot parse string') in Android 2.2 default browser and black berry devices' browers.

JSON.parse works in the browser just not with my JSON string A. Is there a bug in the json parser on this browser?

edit. If I put A into a textArea and copy and paste the string directly into the javascript code.. JSON.parse('[{"key1":"val1",...,"keyn":"valn"},{...},...,{...}]') works.

But, this doesn't work -> JSON.parse($('div').html(A).text());

the older JSON.parse only will parse a object....

try surround like below...

'{ "data" : [{"key1":"val1",...,"keyn":"valn"},{...},...,{...}] }'

if not work... you can try

function getObj(json){
  return (new Function(" return (" + json + "); "))();
}

var value = getObj('[{"key1":"val1",...,"keyn":"valn"},{...},...,{...}]');

try:

var myJSON = eval( yourJSONString );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM