我需要一些帮助,我有以下代码
$producers = simplexml_import_dom($xmlDoc);
$file = fopen('producers.json','w');
foreach( $producers as $producer)
{
$info[] = $producer;
}
$jsonInfo = json_encode($info);
fwrite($file, $jsonInfo);
fclose($file);
此输出
[{“id”:“8”,“name”:“Em \ u00e5mejeriet”,“street”:“Grenv \ u00e4gen 1-3 \ n \ t \ t \ t \ t \ t”,“邮政” “:”577 39 \ n \ t \ t \ t \ t \ t“,”city“:”Hultsfred \ n \ t \ t \ t \ t“,”weburl“:”http://www.emamejeriet。 se“,”logourl“:”http://172.16.206.1/~thajo/1DV449/laboration01/producenter/images/ema.png“},{”id“:”57“,”name“:”\ u00d6lands \ u00f6rtag \ u00e5rd“,”street“:”Stora Fr \ n \ n \ t \“2393 \ n \ t \ t \ t \ t \ t”,“邮政”:“380 62 \ n \ t \ t \ t \ t \ t”, “城市”:“M \ u00f6rbyl ......等等
我需要som帮助来格式化它,我需要摆脱换行符并将其格式化为更易读的json格式。我也无法让UTF-8工作。
/感谢
答案 0 :(得分:0)
试试这个:
PHP 5.4提供了JSON_PRETTY_PRINT选项,用于json_encode()调用。
http://php.net/manual/en/function.json-encode.php
$json_string = json_encode($data, JSON_PRETTY_PRINT);
OR
$string = '{"a":"apple","b":"banana","c":"catnip"}';
$pattern = array(',"', '{', '}');
$replacement = array(",\n\t\"", "{\n\t", "\n}");
echo str_replace($pattern, $replacement, $string);