在php中编辑json字符串
我试图通过javascript通过php编辑数据查询 我的ajax请求看起来像在php中编辑json字符串,php,javascript,json,Php,Javascript,Json,我试图通过javascript通过php编辑数据查询 我的ajax请求看起来像 var totalSearchResult=10; $.ajax({ url:"php/queryManipulation.php", type: 'POST', data: { totalQuery : totalSearchResult, query : '{"data":{"match_all":{}}}'}, success: function(finalList)
var totalSearchResult=10;
$.ajax({
url:"php/queryManipulation.php",
type: 'POST',
data: { totalQuery : totalSearchResult, query : '{"data":{"match_all":{}}}'},
success: function(finalList)
{
alert(finalList);
}
});
<?php
$from=$_POST["totalQuery"];
$qry=json_decode($_POST["query"]);
$qry->from=$from; }?>
我的php代码看起来像
var totalSearchResult=10;
$.ajax({
url:"php/queryManipulation.php",
type: 'POST',
data: { totalQuery : totalSearchResult, query : '{"data":{"match_all":{}}}'},
success: function(finalList)
{
alert(finalList);
}
});
<?php
$from=$_POST["totalQuery"];
$qry=json_decode($_POST["query"]);
$qry->from=$from; }?>
我得到的错误是stdClass类的对象无法转换为string
您应该使用json\u decode($string,true)-因此它将是一个数组。
此处的详细信息:您应该使用json_decode($string,true)-因此它将是一个数组。
此处的详细信息:您可以解码到数组(不确定对象)并重新编码:
$qry = json_decode($_POST['query'], TRUE);
$qry['from'] = 10;
$new_qry = json_encode($qry);
您可以解码到数组(不确定对象)并重新编码:
$qry = json_decode($_POST['query'], TRUE);
$qry['from'] = 10;
$new_qry = json_encode($qry);
编辑:将json_解码返回值从数组更改为对象 您需要在完成编辑后再次编码json。 所以你能做的是:
<?php
$from = $_POST["totalQuery"];
$qry = json_decode($_POST["query"]);
$qry->data->from = $from;
//you will get the new json string
//as the finalList variable in your post callback
echo json_encode($qry);
?>
编辑:将json解码返回值从数组更改为对象 您需要在完成编辑后再次编码json。 所以你能做的是:
<?php
$from = $_POST["totalQuery"];
$qry = json_decode($_POST["query"]);
$qry->data->from = $from;
//you will get the new json string
//as the finalList variable in your post callback
echo json_encode($qry);
?>
你忘了json\u编码
它回来了?为什么我会在你的PHP脚本末尾看到一个额外的
呢?你忘了json\u编码
它回来了?为什么我会在你的PHP脚本末尾看到一个额外的
呢?我得到它的形式是{“数据”:{“匹配所有”:[]},“from”:“10”}。。。它应该像{“data”:{“match_all”:{},“from”:“10”}我已经更改了答案。现在试试。我得到的格式是{“data”:{“match_all”:[]},“from”:“10”}。。。它应该像{“data”:{“match_all”:{},“from”:“10”}我已经更改了答案。现在试试。