Reputation: 1601
Had to re do this as last question was marked to close as duplicate. BUT the "Duplicate" question was about json decoding not encoding and adding to a file. I have also stated in my question the things I have tried, which as it so happens was the accepted answer on the 'duplicate'.
Please do not mark as duplicate again without actually checking it is.
When I try to create a file with some generated JSON data, the result is like so:
"{\"1\":{\"title\":\"Sidemen: The Book\",\"ISBN\":\"1473648165\"
as you can see, it has wrapped everything in "" quotes ""
It has also added in lots of: ' \ ' which I don't want.
This is what I currently have:
$json = json_encode($BookList);
and I create the file in PHP like so:
$fp = fopen($filename, 'w');
fwrite($fp, json_encode($json));
fclose($fp);
I have tried:
$json = json_encode($BookList, JSON_UNESCAPED_SLASHES);
and:
$json = json_encode($BookList, JSON_UNESCAPED_SLASHES);
$json_edited = stripslashes($json);
but nothing seems to be working.
Any ideas?
Update
Book list is generated like so:
$BookList[++$id] = [
'title' => (string) $item->ItemAttributes->Title,
'ISBN' => '' .(string) $item->ItemAttributes->ISBN
];
Upvotes: 0
Views: 868
Reputation: 1075427
You're double-encoding your data, e.g., passing it through json_encode
twice. You have
$json = json_encode($BookList, JSON_UNESCAPED_SLASHES);
// -----^^^^^^^^^^^
and also
fwrite($fp, json_encode($json));
// ---------^^^^^^^^^^^
You only want one of those.
Upvotes: 5