[英]How to parse json data from php to jQuery
I have a curl statement that pulls json formatted array to php. 我有一个curl语句,将json格式的数组拉到php。 I then want to transfer this array to jQuery so that the client side will hold the array.
然后我想将此数组传输到jQuery,以便客户端将保存该数组。 I'm currently using the below method:
我目前正在使用以下方法:
<script>var obj = jQuery.parseJSON( <?php echo var_dump($json_short); ?> );</script>
The client sees something like: 客户看到类似的东西:
<script>var obj = jQuery.parseJSON( array(1) {
["search"]=>
array(50) {
[0]=>
array(6) {
["id"]=>
string(6) "641279"
["description"]=>
string(36) "Instyle - Responsive Portfolio Theme"
["url"]=>
string(69) "http://themeforest.net/item/instyle-responsive-portfolio-theme/641279"
["type"]=>
string(9) "wordpress"
["sales"]=>
string(3) "135"
["rating"]=>
string(3) "4.5"
}
....
}
}
);</script>
Will obj now hold the array? obj现在会持有阵列吗? is this the right way becuase I'm getting an error:
这是正确的方法,因为我得到一个错误:
Uncaught SyntaxError: Unexpected token {
PHP has json_encode function already, you should use that. PHP已经有了json_encode函数,你应该使用它。
Would look like: 看起来像:
<script>
var a = <?php echo json_encode($json_short); ?>;
</script>
你不能使用直接转储,你需要先json_encode:
<script>var obj = <?php echo json_encode($json_short) ?>;</script>
I don't understand what you're trying with <script>var obj = jQuery.parseJSON( <?php echo var_dump($json_short); ?> );</script>
我不明白你在尝试使用
<script>var obj = jQuery.parseJSON( <?php echo var_dump($json_short); ?> );</script>
in PHP try echo json_encode($json_short);
在PHP中尝试
echo json_encode($json_short);
the var_dump
function doesn't dump it as a json object. var_dump
函数不会将其转储为json对象。
use json_encode
instead of var_dump
. 使用
json_encode
而不是var_dump
。
Don't use var_dump first off. 首先不要使用var_dump。 Next make sure you have converted your variable to a json array you have a normal array.
接下来确保已将变量转换为具有普通数组的json数组。
<script>var obj = jQuery.parseJSON( <?php echo json_encode($json_short); ?> );</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.