Lunarovich Posted November 21, 2015 Share Posted November 21, 2015 Hello, I would like to take a JSON file and read its content into a normal Lua string. Even better, I would like to put a JSON text into a Lua table directly, if it's possible. So far, I've found this Lua lib for converting a JSON string into a Lua table and vice versa. However, I don't see how to read a whole file as a string. Thank you in advance! P.S. I know how to read an individual line with this function. Quote Link to comment Share on other sites More sharing options...
Rick Posted November 21, 2015 Share Posted November 21, 2015 http://www.leadwerks.com/werkspace/page/api-reference/_/stream/ http://www.leadwerks.com/werkspace/page/api-reference/_/filesystem/ http://www.leadwerks.com/werkspace/page/api-reference/_/filesystem/filesystemgetappdatapath-r801 Note that file i/o will not work with the game launcher if you care about that. Also you might have to pay attention of where you are reading/writing the file. I don't think it can be part of the zip file as you won't know the pw to open it. So normally these files need to be somewhere else. Quote Link to comment Share on other sites More sharing options...
Lunarovich Posted November 21, 2015 Author Share Posted November 21, 2015 Thank you! Actually, I know the API, I was rather asking if anyone has ready to use solution that demads less tinkering. Quote Link to comment Share on other sites More sharing options...
Rick Posted November 21, 2015 Share Posted November 21, 2015 My question would be why JSON? Is there a specific reason to pick that format? Quote Link to comment Share on other sites More sharing options...
Lunarovich Posted November 21, 2015 Author Share Posted November 21, 2015 Sure! Because that's the format delivered by the third party Anyway, it does not matter if it's JSON. I just want to read in the string as is and have it in the Lua var. Quote Link to comment Share on other sites More sharing options...
Rick Posted November 21, 2015 Share Posted November 21, 2015 Using the example of the API as reference something like the below should do that local stream = FileSystem:ReadFile(path) if (stream==nil)then Debug:Error("Failed to read file.") end local json = nil while (not stream:EOF()) do json = json..stream:ReadLine end stream:Release() Quote Link to comment Share on other sites More sharing options...
Lunarovich Posted November 21, 2015 Author Share Posted November 21, 2015 Thank you Rick. This is a solution I came up with. It's similar to yours. It puts a JSON string into a Lua table: --Load JSON (http://regex.info/blog/lua/json) library JSON = (loadfile "lua/JSON.lua")() -- put the lib in "lua" folder path = "pathToJSON" stream = FileSystem:OpenFile(path) jsonString = "" if (stream) then while (not stream:EOF()) do jsonString = jsonString .. stream:ReadString() end data = JSON:decode(Swinglets.jsonString) else System:Print("Stream not found") end Quote Link to comment Share on other sites More sharing options...
Vladimir Posted June 10, 2021 Share Posted June 10, 2021 On 11/21/2015 at 8:44 PM, Lunarovich said: Thank you Rick. This is a solution I came up with. It's similar to yours. It puts a JSON string into a Lua table: --Load JSON (http://regex.info/blog/lua/json) library JSON = (loadfile "lua/JSON.lua")() -- put the lib in "lua" folder path = "pathToJSON" stream = FileSystem:OpenFile(path) jsonString = "" if (stream) then while (not stream:EOF()) do jsonString = jsonString .. stream:ReadString() end data = JSON:decode(Swinglets.jsonString) else System:Print("Stream not found") end Can you tell me why i get the following error: "attempt to index a nil value (global 'FileSystems')" and also in the path variable i have to initialize the path of the json that i want to parse right ? Thank you ! Quote Link to comment Share on other sites More sharing options...
Thirsty Panther Posted June 10, 2021 Share Posted June 10, 2021 FileSystems should be FileSystem Quote Link to comment Share on other sites More sharing options...
Slastraf Posted June 12, 2021 Share Posted June 12, 2021 Personally I would use leadwerks stream and make my own parser and file system depending on what I need for example names.data foo bar then loop trough all the lines and read all the data into an array Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.