Hello
I’m trying to load a valid Json file and then to convert into a JsonItem (Mac, Desktop). Initially I tried to load my own created Json file (UTF8 saved) that throws an exception on converting to JsonItem. So I headed to https://documentation.xojo.com/topics/file_managment/reading_and_writing_data_in_json_format.html to use the sample there:
[{"players":[{"Name":"Bob","position":"1B"},{"Name":"Tom","position":"2B"}],"Team":"Seagulls"},
{"players":[{"Name":"Bill","position":"1B"},{"Name":"Tim","position":"2B"}],"Team":"Pigeons"},
{"players":[{"Name":"Betty","position":"1B"},{"Name":"Tina","position":"2B"}],"Team":"Crows"}]
I load/read the file this way:
[code]Var f As FolderItem = SpecialFolder.Resource(“testfile”)
Var OpCodeList As Dictionary
If f <> Nil Then
If f.Exists Then
// Be aware that TextInputStream.Open could raise an exception
Var t As TextInputStream
Try
t = TextInputStream.Open(f)
t.Encoding = Encodings.UTF8
// This is working (show json file in textarea)
OutputField.Value = t.ReadAll
// This throws the exception
OpCodeList = ParseJSON(t.ReadAll)
Catch e As IOException
MessageBox("Error accessing file.")
End Try
t.Close
Else
MessageBox(“File could not be found”)
End If
End If[/code]
Thrown Exception:
InvalidJSONEXCEPTION:
ErrorNumber: 0
Message: ■■■■ error: premature EOF ck?P>? Offset: 0 Reason: ■■■■ error: premature EOF ck???P>?
( !@#$% stands for a.r.s.e. (w/o dots) and should probably mean “parse”) :o)
I can load my original Json file into any Json reader on my Mac and they don’t show any invalid structure. Initially I had the file content as variable and it was accepted inside Xojo ( OpCodeList = ParseJSON(JsonString) was working ). So I assume something special/wrong with the file.
I also tried different file types (UTF8, RTF, TXT) but all with the same error message.
Do I have to consider something special/additional in order to read and convert a JSON file?