util.TableToJSON

From Garry's Mod
Jump to: navigation, search
 util.TableToJSON( )

Contents

Description

Converts a table to a JSON string.

WARNING All keys are strings in the JSON format, so all keys will be converted to strings!

Arguments

table table

Table to convert.

Arguments

boolean prettyPrint=false

Format and indent the JSON.

Returns

string

JSON

Examples

Example

Writes the positions and angles of every player to a txt document called playerstuff.txt

local Players = {}
for k, v in pairs(ents.GetAll()) do
	if v:IsPlayer() then -- Is a player
		Players[k] = { pos = v:GetPos(), ang = v:GetAngles() }
	end
end
	
local tab = util.TableToJSON( Players ) -- Convert the player table to JSON
file.CreateDir( "jsontest" ) -- Create the directory
file.Write( "jsontest/playerstuff.txt", tab) -- Write to .txt


Personal tools
Navigation