I'm trying to generate a json using PHP, with data I get from a form. The fact is that I need to use quotes in the json format, and I try to add it using \\" but it don't works. If someone can help me I'd be gratefull!
Here's the code:
$clave = $_GET["laclave"];
$arrayClave = unserialize(base64_decode($clave));
$guardar = array();
foreach ($arrayClave as $value){
$valorA = ("\"".$value."\":\"".$_GET[$value]."\"");
$guardar[] = $valorA;
}
$fp = fopen('datatodo/prueba.json', 'w');
fwrite($fp, json_encode($guardar));
fclose($fp);
Json_encode does everything for you, just give it an array and it'll figure it out :-)
$clave = $_GET["laclave"];
$arrayClave = unserialize(base64_decode($clave));
$fp = fopen('datatodo/prueba.json', 'w');
fwrite($fp, json_encode($arrayClave));
fclose($fp);
From the manual page for json_encode()
:
string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )
Returns a string containing the JSON representation of value.
value
The value being encoded. Can be any type except a resource.
All string data must be UTF-8 encoded.
The only thing you must take care of yourself is generating UTF-8 (if your source data uses a different encoding). This can be accomplished with mb_convert_encoding() or iconv()
You should also test the return value because you don't possibly want to generate files for invalid input:
Returns a JSON encoded string on success or
FALSE
on failure .
It'd be pointless to have a function to generate JSON manually, even in PHP ;-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.