La fonction json_decode() retourne NULL

Résolu
stgcici Messages postés 51 Date d'inscription mardi 29 octobre 2013 Statut Membre Dernière intervention 18 février 2014 - Modifié par stgcici le 28/11/2013 à 16:46
stgcici Messages postés 51 Date d'inscription mardi 29 octobre 2013 Statut Membre Dernière intervention 18 février 2014 - 2 déc. 2013 à 13:20
Bonjour,
J'ai un sérieux problème avec la fonction json_decode().
Il me retourne NULL quand je lui transmet mon fichier json.
Au faite il n'arrive pas à extraire le contenu du fichier json que je lui passe.
Je récupère le contenu du fichier json à l'aide de la fonction file_get_contents("fichier.json") et j'affecte le résultat à une variable.

$data=file_get_contents("fichier.json");

$resultat=json_decode($data,true);

va_dump($resultat);// retourne NULL

Pourtant mon fichier json est bien structuré
SOS aide s'il vous plait.
Voila mon fichier json:

{"orders":[
{
"id_commande":44,
"date":"2013-11-26T10:30:00",
"status":"Waiting",
"customerName":"ok",
"delivery":"ok",
"deliveryCompanyName":"Mp",
"deliveryAddress":"Rennequin ",
"deliveryPostcode":"75",
"deliveryCity":"Paris",
"deliveryCountry":"France",
"billingName":"B S",
"billingCompanyName":"Mp",
"billingAddress":"Rennequin ",
"billingPostcode":"750",
"billingCity":"Paris",
"billingCountry":"France",
"products":[
{
"image":"products_no_image_Medium.jpg",
"title":"Bracelet",
"brand":"HIPANEMA",
"quantity":1.0,
"price":15,
"productId":24,
"offerId":13
}
],
"totalPrice":15,
"productsCount":1
},
{
"id_commande":4462,
"date":"2013-11-26T10:35:00",
"status":"Waiting",
"customerName":"BS",
"deliveryName":"B",
"deliveryCompanyName":"Mp",
"deliveryAddress":"ok",
"deliveryPostcode":"75",
"deliveryCity":"Paris",
"deliveryCountry":"France",
"billingName":"BS",
"billingCompanyName":"Mp",
"billingAddress":"ok ",
"billingPostcode":"75",
"billingCity":"Paris",
"billingCountry":"France",
"products":[
{
"image":"products_no_image_Medium.jpg",
"title":"Tropez",
"brand":"HIPA",
"quantity":2.0,
"price":15,
"productId":25,
"offerId":133522
}
],
"totalPrice":300,
"productsCount":2
}
],
"recordsReturned":2,
"totalRecords":2,
"startIndex":1,
"sort":0,
"pageIndex":1,
"pageSize":50
}

3 réponses

f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 37
1 déc. 2013 à 02:49
$data=file_get_contents("fichier.json"); // <- doit etre UTF-8 SANS BOM pas de EOL/EOLN en fin de fichier

$resultat=json_decode($data); // <- sans TRUE on retourne la version objet (sinon quel intêréts d'utiliser JSON si c'est pour faire un tableau ... autant utiliser CSV sinon)

var_dump($resultat); // <- as tu plutot tenté ceci : var_dump($data);


<?php
$filename = 'fichier.json';

if(file_exists( $filename ))
{
  $content = file_get_contents( $filename );  
  $content = mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true)); 
  $orders  = json_decode( $content );

  var_dump($content);
  var_dump($orders);
}
?>
0
stgcici Messages postés 51 Date d'inscription mardi 29 octobre 2013 Statut Membre Dernière intervention 18 février 2014
2 déc. 2013 à 09:54
Bonjour;
Merci pour ton aide.
J'ai toujours un petit soucis malgré que j'ai adapté ton code à mon projet.
var_dump($content)//retourne le contenu du fichier json
var_dump($orders)// retourne Null (toujours le même probléme)
0
stgcici Messages postés 51 Date d'inscription mardi 29 octobre 2013 Statut Membre Dernière intervention 18 février 2014
2 déc. 2013 à 13:20
salut,
c'est ok maintenant.
Merci pour ton aide
0