【发布时间】:2023-04-09 01:24:01
【问题描述】:
取以下 PHP 数组:
$arr = array(
'about' => 'Ai você fala o seguinte: "- Mas vocês acabaram isso?" Vou te falar: -"Não, está em andamento!" Tem obras que "vai" durar pra depois de 2010. Agora, por isso, nós já não desenhamos, não começamos a fazer projeto do que nós "podêmo fazê"? 11, 12, 13, 14...',
'construction' => 100,
);
当使用 PHP 的 json_encode 解析为 JSON 时,我得到以下字符串:
[{"about": "Ai você fala o seguinte: \"- Mas vocês acabaram isso?\" Vou te falar: -\"Não, está em andamento!\" Tem obras que \"vai\" durar pra depois de 2010. Agora, por isso, nós já não desenhamos, não começamos a fazer projeto do que nós \"podêmo fazê\"? 11, 12, 13, 14...", "construction": 100}]
请注意,原始字符串中的双引号会使用反斜杠进行转义。看起来不错,甚至validates。
当我尝试在 Chrome 或 Safari 中解析 JSON(使用 JSON.parse())时,我在控制台中收到以下错误:
Uncaught SyntaxError: Unexpected number
Firefox 给了我:
SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 39 of the JSON data
从 Firefox 错误中,我收集到字符串中第一个转义的双引号似乎破坏了代码。
如果我在引号之前手动转义 反斜杠,我会得到预期的对象...
如何防止此错误?我是不是做错了什么?
相关代码:
型号:
// Returns an array with basic enterprise information
public function getBasicInfo($destination = null)
{
$response = array(
'id' => $this->id,
'name' => $this->name,
'category' => ! empty($this->category_id) ? $this->category->name : '',
'neighborhood' => ! empty($this->neighborhood) ? $this->neighborhood : '',
'city' => (! empty($this->city) ? $this->city : '') . (! empty($this->state_id) ? (! empty($this->city) ? ', ' : '') . $this->state->abbreviation : ''),
'dorms' => $this->dormitories_text,
'area' => $this->area,
'status' => ! empty($this->status_id) ? $this->status->name : '',
'price' => $this->price > 0 ? 'R$ ' . number_format($this->price, 2, ',', '.') : '',
'installment' => $this->installment > 0 ? 'R$ ' . number_format($this->installment, 2, ',', '.') : '',
'image' => ! empty($this->card_image_id) && is_object($this->card_image) ? $this->card_image->getUrl() : '',
'logo' => ! empty($this->logo_image_id) && is_object($this->logo_image) ? $this->logo_image->getUrl() : '',
'permalink' => $this->getPermalink($destination),
'about' => ! empty($this->about) ? $this->get_html($this->about) : '',
'construction' => $this->getLatestConstructionStageProgress(),
);
return $response;
}
控制器:
// Build data array
$json = array();
foreach ($enterprises as $enterprise) {
$json[] = $enterprise->getBasicInfo(REALTOR_URL);
}
$json = json_encode($json);
// Build data array
$data = array(
'urlOrigin' => $this->url_origin(),
'module' => 'Portal do Corretor - Empreendimentos',
'categories' => \Type::getByType('category'),
'cities' => \Enterprise::getUniqueCities(),
'statuses' => \Type::getByType('status'),
'prices' => \Enterprise::$priceRanges,
'installments' => \Enterprise::$installmentRanges,
'neighborhoods' => $neighborhoods,
'json' => $json,
'filters' => (object) array(
'search' => isset($_POST['search']) && ! empty($_POST['search']) ? $_POST['search'] : null,
'city' => isset($_POST['city']) && ! empty($_POST['city']) ? $_POST['city'] : null,
'neighborhood' => isset($_POST['neighborhood']) && ! empty($_POST['neighborhood']) ? $_POST['neighborhood'] : null,
'category' => isset($_POST['category']) && ! empty($_POST['category']) ? intval($_POST['category']) : null,
'dormitories' => isset($_POST['dormitories']) && ! empty($_POST['dormitories']) ? intval($_POST['dormitories']) : null,
'status' => isset($_POST['status']) && ! empty($_POST['status']) ? intval($_POST['status']) : null,
),
'sectionId' => 'empreendimentos',
);
// Merge default values with the data array
$data = array_merge($data, $this->getDefaultValues());
// Returns the view with the data array
return $this->view(REALTOR_URL . DS . 'empreendimentos', $data);
查看:
<script type="text/javascript">
var enterprises = '<?php echo $json; ?>';
</script>
JavaScript:
if ($('#enterprises') && $('#enterprise-template').length && 'undefined' !== typeof enterprises) {
enterprises = JSON.parse(enterprises);
enterprisesCount = enterprises.length;
}
【问题讨论】:
-
显示你的 js 代码。如果您上面的 json 字符串有效,那么 ELSE 正在破坏它。
-
添加了相关代码sn-ps。如果您还需要其他东西,请告诉我。
-
json 已经是有效的 javascript,所以你可以只使用
var foo = <?php echo $json ?>;。无需转储到'字符串然后单独解析。但是,由于您的字符串已损坏,无论如何它仍然会以其他方式损坏-您是否确认您在任何地方都有正确的字符集?例如如果您在(比如说)win-1252 字符集环境中构建 json,然后转储到 utf-8 页面中,您会得到损坏的字符。 -
@MarcB:你是对的。删除单引号有效,但问题仍然存在。是的。我到处都在使用 UTF-8。您可以在控制台中运行
JSON.parse('[{"about": "Ai você fala o seguinte: \"- Mas vocês acabaram isso?\" Vou te falar: -\"Não, está em andamento!\" Tem obras que \"vai\" durar pra depois de 2010. Agora, por isso, nós já não desenhamos, não começamos a fazer projeto do que nós \"podêmo fazê\"? 11, 12, 13, 14...", "construction": 100}]');吗?如果您遇到同样的错误,我们可以排除编码问题。 -
那是因为您的 json 已损坏。 json IS 有效的 javascript。如果您的 json 字符串有效,那么
var foo = [{....}];将是完全可以接受/有效的 javascript。