从 $_POST 中的 json 读取关联数组

发布于 2024-11-03 19:24:54 字数 648 浏览 0 评论 0原文

我正在使用 jQuery 将 json 对象发布到我的 php 应用程序。

jQuery.post("save.php",JSON.stringify(dataToSend), function(data){ alert(data); });

从 firebug 中提取的 json 字符串看起来像这样

{ "data" : [ { "contents" : "This is some content",
        "selector" : "DIV.subhead"
      },
      { "contents" : "some other content",
        "selector" : "LI:nth-child(1) A"
      }
    ],
  "page" : "about_us.php"
}

在 php 中我试图将其转换为关联数组。

到目前为止,我的 php 代码是

<?php
$value = json_decode(stripcslashes($_POST));
echo $value['page'];
?>

对 ajax 调用的响应应该是“about_us.php”,但它返回空白。

I am using jQuery to post a json object to my php application.

jQuery.post("save.php",JSON.stringify(dataToSend), function(data){ alert(data); });

The json string as pulled from firebug looks like this

{ "data" : [ { "contents" : "This is some content",
        "selector" : "DIV.subhead"
      },
      { "contents" : "some other content",
        "selector" : "LI:nth-child(1) A"
      }
    ],
  "page" : "about_us.php"
}

In php I am trying to turn this into an associative array.

My php code so far is

<?php
$value = json_decode(stripcslashes($_POST));
echo $value['page'];
?>

The response to the ajax call should be "about_us.php" but it comes back blank.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

┈┾☆殇 2024-11-10 19:24:54

如果请求正文不是标准 urlencoded 形式,则不会填充 $_POST

相反,从 只读 php://input 流读取 像这样获取原始请求正文:

$value = json_decode(file_get_contents('php://input'));

$_POST will not be populated if the request body is not in the standard urlencoded form.

Instead, read from the read-only php://input stream like this to get the raw request body:

$value = json_decode(file_get_contents('php://input'));
彩扇题诗 2024-11-10 19:24:54

您可以避免使用 JSON.stringifyjson_decode

jQuery.post("save.php", dataToSend, function(data){ alert(data); });

并且:

<?php
echo $_POST['page'];
?>

更新:

...但如果您确实想使用它们,那么:

jQuery.post("save.php",  {json: JSON.stringify(dataToSend)}, function(data){ alert(data); });

和:

<?php
$value = json_decode($_POST['json']);
echo $value->page;
?>

You can avoid to use JSON.stringify and json_decode:

jQuery.post("save.php", dataToSend, function(data){ alert(data); });

And:

<?php
echo $_POST['page'];
?>

Update:

... but if your really want to use them, then:

jQuery.post("save.php",  {json: JSON.stringify(dataToSend)}, function(data){ alert(data); });

And:

<?php
$value = json_decode($_POST['json']);
echo $value->page;
?>
任性一次 2024-11-10 19:24:54

如果您想要关联数组,请将第二个参数传递为 true,否则它将继续返回对象。

$value = json_decode(stripslashes($_POST),true);

Pass the second argument as true if you want the associative array otherwise it will keep returning object.

$value = json_decode(stripslashes($_POST),true);
无所的.畏惧 2024-11-10 19:24:54

尝试:

echo $value->page;

因为 json_decode 的默认行为是返回 stdClass 类型的对象。

或者,将第二个可选的 $assoc 参数设置为 true

$value = json_decode(stripslashes($_POST), true);
echo $value['page'];

Try:

echo $value->page;

since json_decode's default behaviour is to return an object of type stdClass.

Alternatively, set the second optional $assoc argument to true:

$value = json_decode(stripslashes($_POST), true);
echo $value['page'];
把梦留给海 2024-11-10 19:24:54

看起来 jQuery 可能会以 urlencoded 形式对 javascript 对象进行编码,然后将其填充到 $_POST 中。至少从他们的示例来看。我会尝试将您的对象传递到 post() 中而不对其进行字符串化。

It looks like jQuery might encode a javascript object in urlencoded form then would be populated into $_POST. At least from their examples. I'd try passing in your object into post() without stringifying it.

心安伴我暖 2024-11-10 19:24:54

如果你想使用 json 数据作为关联数组,你可以尝试如下:

<?php 

$json = 'json_data'; // json data

$obj = jsondecode($json, true); // decode json as associative array 

// now you can use different values as 
echo $obj['json_string']; // will print page value as 'about_us.php' 


for example: 

$json = { "data" : [ { "contents" : "This is some content",
    "selector" : "DIV.subhead"
   },
   { "contents" : "some other content",
    "selector" : "LI:nth-child(1) A"
   }
  ],
"page" : "about_us.php"
}

$obj = json_decode($json, true); 

/* now to print contents from data */

echo $obj['data']['contents']; 
 // thats all 
?>

If you want to use json data as an associative array, you can try as following:

<?php 

$json = 'json_data'; // json data

$obj = jsondecode($json, true); // decode json as associative array 

// now you can use different values as 
echo $obj['json_string']; // will print page value as 'about_us.php' 


for example: 

$json = { "data" : [ { "contents" : "This is some content",
    "selector" : "DIV.subhead"
   },
   { "contents" : "some other content",
    "selector" : "LI:nth-child(1) A"
   }
  ],
"page" : "about_us.php"
}

$obj = json_decode($json, true); 

/* now to print contents from data */

echo $obj['data']['contents']; 
 // thats all 
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文