Loads a variable based on a Structured Data Type (SDT) or Business Component from a JSON string received as a parameter.
[&Boolean =] &var.FromJson(&String[, &Messages])
Where:
&String
Attribute or variable string with the JSON content.
&var
Variable based on an SDT or Business Component.
&Messages
Variable based on the GeneXus's Messages data type. In case of an error, it will contain the error information. This Parameter is optional.
Type returned:
Boolean.
It returns False in case of having an error; otherwise, it returns True. The assignment of the return value is optional.
Objects: Procedure, Transaction, Web Panel, Panel
Generators: .NET Framework .NET, Java
The FromJson method loads the &var structure from the &String variable content. The variable's JSON format (&String) must be compatible with the variable based on an SDT or Business Component structure (&var).
Consider a Structured Data Type defined as follows:
SDT1
{
NumericMember: Numeric
CharacterMember: Character
GeoPointMember: GeoPoint
}
Define the following code (for example in a Procedure Source or in a Web Panel / Panel Event):
&VarBasedOnSDT1.FromJson('[{"NumericMember": 0,"CharacterMember":"ValueCharacter","GeoPointMember":"POINT(-56.10 -33.01)"}]')
The above code initializes the &VarBasedOnSDT1 variable.
Note that the GeoPointMember receives a value in WellKnowText Format (WKT). Another alternative, only available when generating for Android, is to pass a Geolocation domain format as parameter, as follows:
&VarBasedOnSDT1.FromJson('[{"NumericMember": 0,"CharacterMember":"VallueCharacter","GeoPointMember":"57.10, 68.9"}]')
Data Type Mapping
The GeneXus basic data types are mapped to JSON data types in this way:
- Character, VarChar, LongVarChar → JSON string
- Boolean → JSON Boolean
- Numeric → JSON Number
- Collections → an ordered sequence of values, comma-separated and enclosed in square brackets
Ignored SDT Properties
The following properties are ignored in the process of serializing an SDT to JSON:
ToJson Method
Structured Data Type (SDT) object