ToJson

From GTA Network Wiki
Jump to: navigation, search

ToJson
Server-Side Function

utilities-terminal.png Server-Side csharp.png C#

Serializes arrays and objects into JSON format.

Syntax

string NAPI.Util.ToJson(object data);

Required Arguments

  • data: The data to be serialized. Should be in object type.

NOTE: This function returns data in string type.

Usage example(s)

Converting a simple string array into json

string[] data = new[] {"test value", "value 2"};
var dataInJson = NAPI.Util.ToJson(data);
//dataInJson = ["test value", "value 2"]

Converting a class object into json

class Person
{
    string Name;
    string[] Aliases;
}
Person person = new Person();
person.Name = "Steve";
person.Aliases = new string[] {"Stevie-o", "'Ole Steve"};
var dataInJson = NAPI.Util.ToJson(person);
/*
dataInJson = {
    "Name": "Steve",
    "Aliases": [
        "Stevie-o",
        "'Ole Steve"
    ]
}
*/

Changelog

Version Description
1.0 Release.

References