Php 如果它对你有用但对我有用。。。我正在通过一个route param使用laravel 4 ajaxing $username = "{username: john}"; public function getAjaxSearchName($username) { $username = json_encode($username); die(var_dump(json_decode($username, true))); }
你应该试试看。它会给你错误信息,并告诉你哪里出了问题。它是在PHP5.5中引入的Php 如果它对你有用但对我有用。。。我正在通过一个route param使用laravel 4 ajaxing $username = "{username: john}"; public function getAjaxSearchName($username) { $username = json_encode($username); die(var_dump(json_decode($username, true))); },php,json,Php,Json,你应该试试看。它会给你错误信息,并告诉你哪里出了问题。它是在PHP5.5中引入的 $foo = "{"action":"set","user":"123123123123","status":"OK"}"; $data = json_decode($foo, true); if($data == null) { throw new Exception('Decoding JSON failed with the following message: '
$foo = "{"action":"set","user":"123123123123","status":"OK"}";
$data = json_decode($foo, true);
if($data == null) {
throw new Exception('Decoding JSON failed with the following message: '
. json_last_error_msg());
}
// ... JSON decode was good => Let's use the data
在应用PHP相关解决方案之前,请验证JSON格式。这可能就是问题所在。试试下面的在线JSON格式验证程序。如果JSON格式无效,请先更正它,因为PHP不会解码无效的JSON字符串
Hi-Olafur,utf8\u encode将一些奇怪的字符写入字符串的开头。utf8_解码写入一个?到字符串的开头。这只是一个解决办法,但它是有效的。我相信有更好的方法:$foo=utf8\u decode($result->data);json_解码(str_替换(“?”,“,$foo),true);这对我来说很有效,所有其他提到的答案都不起作用。我不这么认为。这是一个卷曲的括号。我认为每个JSON结构都应该以一个大括号开始。不,不是大括号-大括号前面有一个字符。我认为SO的格式正试图让这个问题变得很难回答-p->好的,记事本++让我看到这个字符。我的FF3没有显示它。。。非常感谢!:)@这是一个古老的问题/答案,但我在这里找到了一个更好的解决方案来删除UTF-8 BOM:。由于它使用
preg_replace
而不是(mb_uu)substr
,因此它将处理带BOM和不带BOM的字符串。如果您能更新您的答案,那就太好了。;)我找不到一个小撇号。双引号后面的第一个符号是一个花括号。非常感谢Pablo的回答!!你为我节省了很多时间!!干杯,伙计,没想到神奇的引语会引起这样的问题,差点冲着显示器大喊大叫。干杯。@Pablo为什么在get\u magic\u quotes\u gpc()返回true时调用stripslashes()?它对我有效,但在get_magic_quotes_gpc之前添加(!),因此如果get_magic_quotes_gpc不返回true,我将其称为spripslashes()@jey-Hey,任何有效的都可以。在我的例子中,上面的代码修复了我的问题,我仍然需要stripslashes
,即使魔法引号已关闭。有什么解释吗?这没有意义,你需要从JS中使用一个字符串。问题是关于json_encode调用失败。这对存储在mysql utf8表中的json字符串有效。我怀疑在存储到mysql时会发生一些事情。谢谢!!我花了数小时尝试不同的解决方案,这是唯一有效的方法。这也是对我有效的方法。它需要解码存储在Wordpress中的JSON。伙计…你让我开心。这个问题快把我累死了!正如我们的一位用户刚刚遇到的:PHP.net是错误的,这是在5.3中引入的,因此可以确认@mdoliyat这对我帮助很大!这对我有效,不是UTF-8编码中的BOM,而是添加了一些斜杠。我有同样的问题,对我有效。
var_dump($data):
NULL
$foo = utf8_encode($foo);
$data = json_decode($foo, true);
"{"action":"set","user":"123123123123","status":"OK"}"
string(62) "{"action":"set","user":"123123123123","status":"OK"}"
^
|
This is the UTF-8 BOM
if(get_magic_quotes_gpc()){
$param = stripslashes($_POST['param']);
}else{
$param = $_POST['param'];
}
$param = json_decode($param,true);
json_decode( $so, true, 9 );
$json_errors = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
);
echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
var myJsVar = <?php echo json_encode($var); ?> ; -------> NOT WORKING
var myJsVar = '<?php echo json_encode($var); ?>' ; -------> WORKING
$my_array = json_decode(str_replace ('\"','"', $json_string), true);
$str = mb_convert_encoding($str,'UTF-8','UTF-16');
//HRT - SIGNED
//20130116
//verifica se um num assoc deco é valido
ob_start();
require('/nusoap.php');
$aResponse['SimpleIsMemberResult']['IsMember'] = FALSE;
if(!empty($iNumAssociadoTmp))
{
try
{
$client = new soapclientNusoap(PartnerService.svc?wsdl',
array(
// OPTS
'trace' => 0,
'exceptions' => false,
'cache_wsdl' => WSDL_CACHE_NONE
)
);
//MENSAGEM A ENVIAR
$sMensagem1 = '
<SimpleIsMember>
<request>
<CheckDigit>'.$iCheckDigitAssociado.'</CheckDigit>
<Country>Portugal</Country>
<MemberNumber">'.$iNumAssociadoDeco.'</MemberNumber>
</request>
</SimpleIsMember>';
$aResponse = $client->call('SimpleIsMember',$sMensagem1);
$aData = array('dados'=>$aResponse->xpto, 'success'=>$aResponse->example);
}
}
ob_end_clean();
return json_encode($aData);
$out = curl_exec($curl);
$out = utf8_encode($out);
$out = str_replace("?", "", $out);
if (substr($out,1,1)!='{'){
$out = substr($out,3);
}
$arResult["questions"] = json_decode($out,true);
$result = mb_convert_encoding($result,'UTF-8','UTF-8');
$result = json_decode($result);
'\'title\' error ...'
$json = json_decode(stripslashes($response));
if (json_last_error() == 0) { // you've got an object in $json}
$username = "{username: john}";
public function getAjaxSearchName($username)
{
$username = json_encode($username);
die(var_dump(json_decode($username, true)));
}
$foo = "{"action":"set","user":"123123123123","status":"OK"}";
$data = json_decode($foo, true);
if($data == null) {
throw new Exception('Decoding JSON failed with the following message: '
. json_last_error_msg());
}
// ... JSON decode was good => Let's use the data