下面的代码是返回的JSON
[{"one":"id","two":"id","three":"id"},{"one":"id","two":"id","three":"id"}]
下面的代码是返回的JSON(没有数组括号)
的理想结果{"one":"id","two":"id","three":"id"},{"one":"id","two":"id","three":"id"}
下面的代码是将数组转换为JSON格式
include('connect-db.php');
$result = mysql_query("SELECT * FROM patientvaccinedetail");
$specific = [];
while($row = mysql_fetch_array( $result ,MYSQL_ASSOC)) {
echo "<tr>";
echo '<td width="100px">' . $row['id'] . '</td>';
echo '<td width="200px">' . $row['patientid'] . '</td>';
echo '<td width="200px">' . $row['vaccineid'] . '</td>';
//**********Convert the array into json*******************
$specific[] = ["one" => $row["id"],
"two" => $row["patientid"],
"three" => $row["vaccineid"]];
$result = json_encode($specific,JSON_UNESCAPED_UNICODE);
echo $result;
echo "</tr>";
echo "</table>";
?>
要将请求发送到API,我使用Guzzle API所需的格式为{xx:xx},{xx:xx} 没有方括号,任何想法如何使用PHP删除它。 提前致谢
$client = new Client([
'headers' => ['Content-Type' => 'application/json',
'Token' => $token]
]);
$response = $client->post('http://localhost:91/Religious',
['body' => ***Where the json will be place***]
);
答案 0 :(得分:6)
我在deceze ♦与trim()
的第一篇文章的评论中看到了一个很好的解决方案。
$yourJson = trim($yourJson, '[]');
您还可以使用正则表达式:
// if nothing is found, your json has already no brackets or is invalid.
if (preg_match('/^\[(.+)\]$/', $yourJson, $new))
{
/**
* $new[0] = $yourJson
* $new[1] = what's in the parenthesis
*/
$yourJson = $new[1];
}
或者,您可以使用substr()
:
$yourJson = substr($yourJson, 1, strlen($yourJson) - 2);
修改强>
如果它在请求正文格式中显示:application/json
,我认为您不必删除括号。你有没有试过它们?
答案 1 :(得分:2)
您正在尝试将PHP数组转换为JSON数组,因此php将结果包装在方括号中,这是完全正常的。如果您需要JSON对象,那么我建议在json_encode中使用JSON_FORCE_OBJECT
作为附加选项(参数)。
$object = json_encode($array, JSON_FORCE_OBJECT);
答案 2 :(得分:0)
试试这个
str_replace(array('[', ']'), '', htmlspecialchars(json_encode($result), ENT_NOQUOTES));
答案 3 :(得分:0)
您可能需要在json解码后使用implode函数。例如:
implode(',',json_decode($ text_from_db或$ array))
例如$ text_from_db = [“大米”,“豆”]
结果将是-米饭,豆子
答案 4 :(得分:0)
我已经添加了关于OP正在尝试执行的操作的注释。但是,这是我为这个问题的未来访客提供的解决方案。
class Thing implements \JsonSerializable
{
private $name;
private $description;
private $code;
/**
* Get the value of name
*/
public function getName()
{
return $this->name;
}
/**
* Set the value of name
*
* @return self
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get the value of description
*/
public function getDescription()
{
return $this->description;
}
/**
* Set the value of description
*
* @return self
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get the value of code
*/
public function getCode()
{
return $this->code;
}
/**
* Set the value of code
*
* @return self
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
public function jsonSerialize()
{
$vars = get_object_vars($this);
return $vars;
}
}
$thing = new Thing();
$thing->setName('Name');
$thing->setDescription('Description');
$thing->setCode('Code');
$results['items'] = [
'thing1' => $thing,
'thing2' => $thing
];
echo json_encode($results);
这是输出
{
"items": {
"thing1": {
"name": "Name",
"description": "Description",
"code": "Code"
},
"thing2": {
"name": "Name",
"description": "Description",
"code": "Code"
}
}
}
答案 5 :(得分:0)
您可以查看下面的示例 trim($ data,'[]')
答案 6 :(得分:0)
这将删除外包装,而不会打扰任何内包装:
$wrapped = <<<EOD
[{"one":"id","two":"id","three":"id"},{"one":"id","two":"id","three":"id"}]
EOD;
$array = json_decode($wrapped, true);
$unwrapped = json_encode($array, JSON_FORCE_OBJECT);
print_r($unwrapped);
// {"0":{"one":"id","two":"id","three":"id"},"1":{"one":"id","two":"id","three":"id"}}
答案 7 :(得分:-1)
我的建议是使用JSON字符串并执行String操作来删除所有那些不需要的字符。
执行此操作的原因是,所需的输出不是有效的JSON,并且API不会接受。但是,根据您的需要,我建议您使用json_encode
将数组转换为json,然后执行字符串操作以将其转换为所需的输出。