假设我有这样的JSON:
{
id: 1,
somevalue: "text"
}我想通过PHP函数json_encode来创建这个JSON。我可以很容易地将这个JSON转换为:
{
"id": "1",
"somevalue": "text"
}或者,使用JSON_NUMERIC_CHECK格式,其中"id“将是数字,但"somevalue”可以是数字或文本,具体取决于其内容。
如何才能使JSON中的"somevalue“始终为文本格式(带引号)。我会用其他语言来解析它,这一点很重要。
发布于 2012-11-21 05:24:17
要使somevalue始终为“文本”格式,请执行以下操作:
$somevalue1 = 1;
$somevalue2 = "text";
$json1 = array("id" => 1, "somevalue" => (string) $somevalue1);
$json2 = array("id" => 1, "somevalue" => (string) $somevalue2);
echo json_encode($json1); // outputs {"id":1,"somevalue":"1"}
echo json_encode($json2); // outputs {"id":1,"somevalue":"text"}https://stackoverflow.com/questions/13482200
复制相似问题