1

Hi I've created json object like that:

{"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}]} 

and I would like to create a file and put json to it, how can I do it?

2
  • How did you "create" the above JSON? Consider saving it into a file? Commented Jun 20, 2014 at 9:31
  • It is repsone from database and I use json_encode() function. I need to save it to file, because it will improve access time from ajax Commented Jun 20, 2014 at 9:37

1 Answer 1

7

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

Let's say that you have $json which contains your json string.

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

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

Or you could use PHP functions, such as file_put_contents

Sign up to request clarification or add additional context in comments.

2 Comments

And a path should be for example "AcmeDemoBunle/Data/my.json ?
Nop. This is the path on the filesystem. For example 'c:\\Windows' (absolute path) or 'tmp' (relative path). You can use thinks like $this->get('kernel')->getRootDir(); to get the Symfony2 dir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.