0

Hi i need some help i have the following code

    $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);

this outputs

[{"id":"8","name":"Em\u00e5mejeriet","street":" Grenv\u00e4gen 1-3\n\t\t\t\t\t","postal":" 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\u00f6 2393 \n\t\t\t\t\t","postal":" 380 62\n\t\t\t\t\t","city":" M\u00f6rbyl...and so on

I need som help to format it, i need to get rid of the newlines and format it to more readable json format. aswell I cannot get the UTF-8 to work.

/thanks

4

1 回答 1

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);

或者

 $string = '{"a":"apple","b":"banana","c":"catnip"}';
 $pattern = array(',"', '{', '}');
 $replacement = array(",\n\t\"", "{\n\t", "\n}");
 echo str_replace($pattern, $replacement, $string);
于 2012-11-19T10:30:17.847 回答