Jump to content

retrieve info from json


SimpleNoob

Recommended Posts

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...