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?

Edited by lufa20
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.