lufa20 Posted April 26, 2013 Share Posted April 26, 2013 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? Link to comment https://forums.phpfreaks.com/topic/277335-reading-json-variable-from-a-file-to-string/ Share on other sites More sharing options...
lemmin Posted April 26, 2013 Share Posted April 26, 2013 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 https://forums.phpfreaks.com/topic/277335-reading-json-variable-from-a-file-to-string/#findComment-1426744 Share on other sites More sharing options...
lemmin Posted April 26, 2013 Share Posted April 26, 2013 trim(), maybe? Link to comment https://forums.phpfreaks.com/topic/277335-reading-json-variable-from-a-file-to-string/#findComment-1426747 Share on other sites More sharing options...
requinix Posted April 26, 2013 Share Posted April 26, 2013 Those leading characters are the UTF-8 BOM. How is this file created? Making sure that doesn't include the BOM is the easiest way to fix this. Link to comment https://forums.phpfreaks.com/topic/277335-reading-json-variable-from-a-file-to-string/#findComment-1426760 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.