繁体   English   中英

PHP json_decode从字符串返回null

[英]PHP json_decode returns null from string

现在,我正在做一个应用程序,它将来自Google Maps的一系列坐标保存到隐藏的输入中,然后从表单向PHP获取数据,它的输出正常,但是当我尝试读取字符串并解码JSON时,它返回空值。

现在,这是我的Javascript代码:

var bounds = []; 
var bound = {};
polygon.getPaths().forEach(function(punto, indice){
     punto.forEach(function(puntoX, indice){
           bound = {'lat': puntoX.lat(), 'lng': puntoX.lng()};
           bounds.push(bound);

     });
});
$('#bounds').val(JSON.stringify(bounds));

HTML代码:

<form class="signup-block" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) . "?c=create"; ?>">
<button id="save" class="button success disabled" type="submit" disabled>Crear Geocerca</button>
<br>
<h3>Datos de la geocerca</h3>
<label for="nombre">Nombre de la geocerca:</label>
<input type="text" id="nombre" name="nombre" required />
<label for="tipo">Tipo de geocerca:</label>
<input type="text" id="tipo" name="tipo" readonly />
<div id="registro"></div>
<input id="bounds" type="hidden" name="bounds" />

这是由JS赋值后的值的边界输入

<input id="bounds" type="hidden" name="bounds" value="[{&quot;lat&quot;:-2.1585226452743442,&quot;lng&quot;:-79.8945227265358},{&quot;lat&quot;:-2.1670138304221926,&quot;lng&quot;:-79.9018183350563},{&quot;lat&quot;:-2.176963037271158,&quot;lng&quot;:-79.8945227265358},{&quot;lat&quot;:-2.1733607458083646,&quot;lng&quot;:-79.875468313694},{&quot;lat&quot;:-2.1587799546129793,&quot;lng&quot;:-79.8783865571022}]">

PHP代码:

$bounds =filter_input(INPUT_POST,"bounds", FILTER_SANITIZE_STRING);
if (!$bounds) {
     $this->error['error'] = 'Parametros incorrecto';
}else{
     $coords = json_decode($bounds);
     echo var_dump($bounds);//Outputs something
     echo var_dump($coords);//Outputs null

我已经检查过了,并且$ bounds变量获取了值,但是它无法将它们读取为JSON,我在做什么错呢?

这是两个var_dumps的输出$ bounds的输出

string(408) "[{"lat":-2.1581795660883976,"lng":-79.90096002817154},{"lat":-2.171559596852916,"lng":-79.9018183350563},{"lat":-2.176448424731824,"lng":-79.89057451486588},{"lat":-2.1662419064644722,"lng":-79.88053232431412},{"lat":-2.156206859267206,"lng":-79.8930636048317},{"lat":-2.15835110569104,"lng":-79.90164667367935}]"

$ coords的输出

NULL

如果其他任何人都和我一样,则问题是HTML输出将所有的“和”打印为html实体,因此首先需要这样做

$bounds = html_entity_decode($bounds);
$coords = json_decode($bounds, true);
echo var_dump($coords);

我花了很长时间才以不同的方式分析页面输出

您可以尝试以下方法:jsondecode($ bounds,true)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM