SimpleNoob Posted February 24, 2016 Share Posted February 24, 2016 Hello, i done a function to save data into a json file, but how i can retrieve that data and save it into an array to each line? This is the code to save data: local veht = {} function saveVehicle(player,cmd,ptarget) local ptarget = ptarget or false local veh = getPedOccupiedVehicle(player) if (veh) then local x,y,z = getElementPosition(veh) local rx,ry,rz = getElementRotation(veh) local id = getElementModel(veh) veht = {["id"] = id, ["x"] = x,["y"] = y,["z"] = z,["rx"] = rx,["ry"] = ry,["rz"] = rz,["ptarget"] = ptarget} local json = toJSON(veht) file = fileOpen("test.json") if file then fileSetPos(file,fileGetSize(file)) fileWrite(file,"\n"..json.."\n") fileClose(file) end end end addCommandHandler("addveh",saveVehicle) and i tried to do a code for test the retrieving but i cant: function getData() local fileo = fileOpen("test.json") if file then local data = fromJSON("id") outputConsole(tostring(tables)) end end addCommandHandler("getdata",getData) how can i retrieve each line into a different array, or load all in one time? Link to comment
SimpleNoob Posted February 24, 2016 Author Share Posted February 24, 2016 someone? :c Link to comment
SpecT Posted February 24, 2016 Share Posted February 24, 2016 Loop the table (data) ? for i, data in pairs(data) do -- etc end Link to comment
SimpleNoob Posted February 24, 2016 Author Share Posted February 24, 2016 i did this: function getData() local fileo = fileOpen("test.json") if fileo then local data = fromJSON(fileRead(fileo,fileGetSize(fileo))) local info = unpack(data) for i,v in ipairs(info) do outputConsole(tostring(info[i])) end end end addCommandHandler("getdata",getData) [ [ [ 429, -2004.267578125, -388.42578125, 58.19026947021484, 340.86181640625, 347.5689697265625, 333.8140869140625, false ], [ 429, -2000.91015625, -381.4013671875, 40.339599609375, 240.677490234375, 3.7188720703125, 339.521484375, false ], [ 429, -1999.4091796875, -378.3544921875, 25.79038238525391, 345.78369140625, 4.41650390625, 358.2366943359375, false ] ] ] it only retrieves the first array of the json, how i can get all? Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now