Garry's Mod Wiki

util.TableToJSON

string util.TableToJSON( table table, boolean prettyPrint = false )

Description

Converts a table to a JSON string.

All integers will be converted to decimals (5 -> 5.0).
All keys are strings in the JSON format, so all keys will be converted to strings!
This will produce invalid JSON if the provided table contains nan or inf values.

Arguments

1 table table
Table to convert.
2 boolean prettyPrint = false
Format and indent the JSON.

Returns

1 string
A JSON formatted string containing the serialized data

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

Page Links


Special Pages


Wikis

?

Render Time: 38ms

Session 0
DB GetPage 4
Generate Html 11
SaveChanges 10
Render Body 0
Render Sidebar 11