0

I have a JSON file, but I can't decode it like any other JSON file. Where am I making a mistake?

$jsonData = '[[[],[{"file_id":"2_U_3","sub_file_id":"2_U_3_1","option_file":[0,3.44827586207],"file_votes":1}],[],[],[],[]],{}]';

$jsonDec = json_decode($jsonData, TRUE);

echo $jsonDec["file_id"] . " = " . $jsonDec["option_file"][2];

Result: empty screen

Editor
  • 622
  • 1
  • 11
  • 24
  • 1
    You have a syntax error on the `$jsonData` assignment line, because you're using double quotes as the string delimiter and also inside the string. – Barmar May 08 '19 at 20:50
  • 2
    Use single quotes around the string: `$jsonData = '[[[],[{"file_id":"1_697","sub_file_id":"2_48281",option_file":[0,0,57,28,14,0]}]';` – Barmar May 08 '19 at 20:50
  • I made a nail error when editing the JSON file for the question. The original file does not have a nail error. I realized there was a missing part in JSON and I made the editing. I also looked at your profile. You're like PHP & JS professor. :) @Barmar – Editor May 08 '19 at 20:57
  • `option_file` only has 2 elements, so the indexes are `0` and `1`. What are you expecting to print with `[2]`? – Barmar May 08 '19 at 20:59
  • `echo $jsonDec[0][1][0]['file_id'] . " = " . $jsonDec[0][1][0]["option_file"][1];` prints `2_U_3 = 3.44827586207` – Barmar May 08 '19 at 21:00
  • Yes! I told you. You're a professor! I'm waiting for your post to accept dear professor @Barmar – Editor May 08 '19 at 21:04
  • 1
    I'm not posting an answer, I already closed the question as a duplicate of a general question that shows how to solve this in general. – Barmar May 08 '19 at 21:05
  • I understand, thank you for your time! @Barmar – Editor May 08 '19 at 21:08

0 Answers0