Jump to content

Reading JSON variable from a file to string


lufa20

Recommended Posts

Everything works when I assigned JSON variable into a string manually, but when I tried to read JSON variable from a file nothing shows up. Why?

 

WORKS

$string= json_decode('{"RubricCriterion":[{"num":1,"value":"0","previous_version":null,"position":1,"name":"Writing","description":"Grammar, spelling, sentence structure, organization, clarity\n\n\n\n25","rubric":352226,"criterion_scales":[10970777,10970781,10970785,10970789],"id":3420113},{"num":2,"value":"0","previous_version":null,"position":2,"name":"APA","description":"APA Style in paper narrative and references\n\n\n20","rubric":352226,"criterion_scales":[10970778,10970782,10970786,10970790],"id":3420114},{"num":3,"value":"0","previous_version":null,"position":3,"name":"References","description":"Quality of references is scholarly, current or classic references included; wide variety of primary sources\n\n10","rubric":352226,"criterion_scales":[10970779,10970783,10970787,10970791],"id":3420115},{"num":4,"value":"0","previous_version":null,"position":4,"name":"Framework","description":"Relation to............and so on

Doesn't Work

$file = "MyRubric.rbc";
$string= json_decode(file_get_contents($file, true));

echo file_get_contents($file); 

{"RubricCriterion":[{"num":1,"value":"0","previous_version":null,"position":1,"name":"Writing","description":"Grammar, spelling, sentence structure, organization, clarity\n\n\n\n25","rubric":352226,"criterion_scales":[10970777,10970781,10970785,10970789],"id":3420113},{"num":2,"value":"0","previous_version":null,"position":2,"name":"APA","description":"APA Style in paper narrative and references\n\n\n20","rubric":352226,"criterion_scales":[10970778,10970782,10970786,10970790],"id":3420114},{"num":3,"value":"0","previous_version":null,"position":3,"name":"References","description":"Quality of references is scholarly, current or classic references included; wide variety of primary sources\n\n10","rubric":352226,"criterion_scales":[10970779,10970783,10970787,10970791],"id":3420115},{"num":4,"value":"0","previous_version":null,"position":4,"name":"Framework","description":"Relation to ...... and so on

I am thinking it has to do with the beginning of the file junk stuff, though none show in notepad. Any way I can target/skip to JSON variable for reading?

I've had this exact same problem! If only I could remember how I fixed it. I'm currently letting my computer search for "json_decode" in my archives.

 

In the mean-time, something like this should get rid of any leading characters:

substr($string, strpos($string, '{'));

I seem to remember feeling stupid when I figured out what was causing it for me. That solution should work, but I will let you know if I find anything else.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.