0

Here is the litteral String displayed on front side when making an ajax call request.

'{
  "nogescom": "",
  "aa": "",
  "acc": "",
  "fournisseur": "001501",
  "semaineEnt": "",
  "debutPeriodeDep": "",
  "finPeriodeDep": "",
  "codepro": "",
  "statutCde": "",
  "statutBieCde": "",
  "isfromgnx": false,
  "usrcreatemodif": false,
  "nocde": "",
  "debutPeriodeCrea": "",
  "finPeriodeCrea": "",
  "fam": "",
  "sfam": "",
  "ssfam": "",
  "entrepot": "",
  "statutDepart": "",
  "saison": "",
  "portDepart": ""
}'

So, this literal String is sent to the back side in PHP 5.4

I'm trying to this on Back Side:

$json = new Services_JSON();

$criteresRecherche= (object) $json->decode($elements);
var_dump($criteresRecherche->fournisseur);

But the var_dump function displays null, I don't understand why!

5
  • 2
    first step is to var_dump the $criteresRecherche var to make sure it returns what you're expecting Commented Oct 17, 2018 at 9:52
  • 2
    ...and before that the $elements var too, to make sure that its contents is really a json string Commented Oct 17, 2018 at 9:54
  • we don't know what your "decode" function does. Does it call the regular json_decode() function in the background? Certainly if you use the normal function, your code would work fine. Demo: sandbox.onlinephpfunctions.com/code/… Commented Oct 17, 2018 at 9:58
  • 2
    Don't use that function. That PEAR package is 7 years old. Use this: php.net/manual/en/function.json-decode.php Commented Oct 17, 2018 at 9:59
  • 1
    Off topic : PHP 5.4 is end of life since 2015. PHP 5.x and 7.0 will become end of life january 1st. You should consider updating to PHP 7.1 or 7.2. source Commented Oct 17, 2018 at 10:03

2 Answers 2

3

$json = json_decode($elements); //without the true parameter

This will create an stdClass object that you can access like:

$json->fournisseur

json_decode is supported from php version 5.4 and above so it should work for you.

If you want to create an array and not an object just add truein the json_decode()

$json = json_decode($elements,true);

and of course you access it like:

$json['fournisseur'];
Sign up to request clarification or add additional context in comments.

Comments

0

Here is the final answer :

$elements = json_decode($elements);
$criteresRecherche = json_decode($elements->criteres);

1 Comment

seems like the answer was done by pr1nc3 first - perhaps accept their post and delete this one?

Your Answer

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