json_decode到数组,不起作用 [英] json_decode to array, not working

查看:373
本文介绍了json_decode到数组,不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个变量$a

var_dump($a)

 array(3) { [0]=> string(10) "designer" [1]=> string(8) "director" [2]=> string(10) "Freelancer"} 

我正在用ajax(jquery)发送此邮件

i am sending this in ajax(jquery)

jquery

  data: 'form=<?php echo json_encode($a); ?>',

在另一个我正在做的php文件中

and in the other php file i am doing

$send = $_POST[form];

$b = json_encode($send);

$c = json_decode($b, true);
var_dump($c);

输出将是:

string(xx) "[\"designer\",\"director\",\"Freelancer\"]" 

但是,echo $c[0]显示以下内容:[,应为"designer"

but, echo $c[0] show this: [ and should be "designer"

有什么帮助吗?

也已经尝试过

$send = $_POST['form'];

$c = json_decode($send, true);

var_dump($c);

output: `null`

推荐答案

因为$c实际上是字符串"[\" designer \,\" director \,\" Freelancer \]",而不是数组["designer", "director", "Freelancer"].看来您要在内容上调用json_encode两次,并在json_decode上调用一次.

Because $c is actually the string "[\"designer\",\"director\",\"Freelancer\"]", and not the array ["designer", "director", "Freelancer"]. It looks like you're calling json_encode on your content twice, and json_decode once.

  1. form=<?php echo json_encode($a); ?>将对您的内容进行一次编码, 在通过电线发送之前.
  2. $send = $_POST[form];将获得该内容(已经 json_encoded).
  3. $b = json_encode($send);将每秒对相同内容进行编码 时间.
  4. $c = json_decode($b, true);将对其进行解码.
  1. form=<?php echo json_encode($a); ?> will encode your content once, before sending it over the wire.
  2. $send = $_POST[form]; will get that content (already json_encoded).
  3. $b = json_encode($send); will encode that same content a second time.
  4. $c = json_decode($b, true); will decode it.

这将使您的内容仍被编码.我不太确定步骤3的要点是什么,在我看来,删除它应该可以解决您的问题.

This will leave you with your content still encoded. I'm not quite sure what the point of step 3 is, and it looks to me that removing it should solve your problem.

根据

Since you've updated the question stating that you get null if you try the proposed solution, according to the PHP documentation for json_decode:

如果无法解码json或编码的数据,则返回

NULL 比递归限制还深.

NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

您应确保将数据设置为所需的精确值,因为在这种情况下,我认为您的递归级别不会太深(从提供的数据来看,似乎没有任何递归值) ).

You should make sure that the data is set to exactly what you want, as I don't think your recursion level is too deep in this case (from the data you've given, it appears as if there is none whatsoever).

这篇关于json_decode到数组,不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆