簡體   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