0
Next, I need to make a json file with a stock of products and a page to add them to JSON.
I got it with PHP through json_encode()
add an array, but the problem: When I add an array, it n writes the comma to separate the items and does not write the opening and closing of the json. my code:
if (!empty($_GET)) {
var_dump($_GET); //Visualiza a variavel
$newProduct = array(
"nome" => $_GET["nome"],
"preco" => $_GET["preco"]);
$dados=$newProduct;
$dadosJSON = json_encode($dados);
$fp = fopen("contatos.json","a");
$escreve = fwrite($fp,$dadosJSON);
fclose($fp); }
How does it look:
{"nome":"Produto teste 1","preco":"9999"}{"nome":"Produto teste 2","preco":"99998"}
How it should look:
[{"nome":"Produto teste 1","preco":"9999"},{"nome":"Produto teste 2","preco":"99998"}]
If you can do it in JS without a problem... Some light?