2017-07-01 126 views
1

我現在寫的Android應用程序和應用程序將使用JSON(UTF-8):PHP無法讀取UFT8 JSON

http://.....xxx.php?json=%7B%22name%22%3A%22peterchan%22%2C%22phone%22%3A%2212345678%22%2C%22password%22%3A%226kxhSJM6iLB0kZ9LZGCEUQ%3S%3D%0A%22%7D

xxx.php是這樣的:

<?php 
    header("Content-Type:text/html; charset=utf-8"); 
    ini_set('default_charset', 'utf-8'); 
    $json = $_GET["json"]; 
    $obj = json_decode($json); 
    $name = $obj -> {"name"}; 
    $phone = $obj -> {"phone"}; 
    $password = $obj -> {"password"}; 
    printf($json); 
?> 

但它返回: Warning: printf():參數太少xxx.php

任何人都可以幫我嗎?請

+0

如果你想創建一些東西,你應該使用'POST' ...和'GET'來檢索... –

+0

我可以舉一些例子嗎?對於android 我無法找到一個工作示例與「放」JsonObject –

+0

你需要什麼使用'printf()'? –

回答

2

您應該驗證json_decode不返回null並檢查錯誤,如果這樣使用json_last_error_msg

您的密碼以新行(也許它本不應該存在),這是結束percent-encoded%0A這是無效的因爲in JSON newlines must be escaped as \n這將是百分比編碼爲%5Cn

演示:https://3v4l.org/vTM2r

$broke = '%7B%22name%22%3A%22peterchan%22%2C%22phone%22%3A%2212345678%22%2C%22password%22%3A%226kxhSJM6iLB0kZ9LZGCEUQ%3S%3D%0A%22%7D'; 
$fixed = '%7B%22name%22%3A%22peterchan%22%2C%22phone%22%3A%2212345678%22%2C%22password%22%3A%226kxhSJM6iLB0kZ9LZGCEUQ%3S%3D%5Cn%22%7D'; 

var_dump(urldecode($broke)); 
// string(80) "{"name":"peterchan","phone":"12345678","password":"6kxhSJM6iLB0kZ9LZGCEUQ%3S= 
// "}" 

var_dump(json_decode(urldecode($broke))); 
// NULL 

var_dump(urldecode($fixed)); 
// string(81) "{"name":"peterchan","phone":"12345678","password":"6kxhSJM6iLB0kZ9LZGCEUQ%3S=\n"}" 

var_dump(json_decode(urldecode($fixed))); 
// object(stdClass)#1 (3) { 
// ["name"]=> 
// string(9) "peterchan" 
// ["phone"]=> 
// string(8) "12345678" 
// ["password"]=> 
// string(27) "6kxhSJM6iLB0kZ9LZGCEUQ%3S= 
// " 
// }