在Symfony 2中创建json文件

时间:2014-06-20 09:28:36

标签: json symfony

您好我已经创建了json对象:

{"ask":[{"rate":"2110.00000000","amount":"2.00000000","price":"4220.00000000","degree":0.2},{"rate":"1800.00000000","amount":"5.00000000","price":"9000.00000000","degree":0.5},{"rate":"20.00000000","amount":"8.00000000","price":"160.00000000","degree":0.8}],"bid":[{"rate":"1700.00000000","amount":"0.50000000","price":"1700.00000000","degree":0.5}]} 

我想创建一个文件并将json放到它上面,我该怎么做?

1 个答案:

答案 0 :(得分:5)

You could use the Filesystem Component, and the dumpFile function.

假设您有$json,其中包含您的json字符串。

$fs = new \Symfony\Component\Filesystem\Filesystem();

try {
    $fs->dumpFile('path/to/my/file.json', $json);
}
catch(IOException $e) {
}

或者您可以使用PHP函数,例如file_put_contents