user881703
user881703

Reputation: 1199

PHP array JSON encode and that object decode inside ExtJs

I have php array as bellow

$php_arr = json_encode(array('1'=>'"data1', '1'=>'data2'));

that json object i try to access inside Extjs as bellow

var test = Ext.JSON.decode()(<?php echo $php_arr; ?>);

but that gave to me error as Uncaught Ext.JSON.decode(): You're trying to decode an invalid JSON String:

because of JSON object break "data1

How can I get this JSON object decoded in ExtJs without lose "?

Upvotes: 0

Views: 695

Answers (1)

Oleksandr T.
Oleksandr T.

Reputation: 77522

Try this

var test = Ext.JSON.decode(<?php echo $php_arr; ?>);

and you must escape " like so

$php_arr = json_encode(array('1'=>'\"data1', '2'=>'data2'));

Also in PHP you can use addslashes, like so

$php_arr = json_encode(array('1'=>'"data1', '2'=>'data2'));
$php_arr = addslashes($php_arr);

Upvotes: 2

Related Questions