Description
Loads a JSON file to the JSONParser or JSONPackage objects.
Applies to
Required JSON format
-
For JSONParser object: The file content that can be loaded by the JSONParser object must be JSON-formatted.
-
For JSONPackage object: The file content that can be loaded by the JSONPackage object must be an object which contains a set of key/value pairs where key is the name of a JSONObjectItem-type object (corresponding to the data being added into the package, such as "d_department" and "d_employee_syntax") and the value for the key can be a string, object, or array in the following formats: plain JSON, or DataWindow JSON.
Here is the structure of this JSON format:
{ "KEY1":VALUE1, "KEY2":VALUE2, "KEY3":VALUE3… }
Here is an example of this JSON format:
{ "d_department": {"department_id":1, "name":"developer"}, "d_employee": [{"empoyee_id":1, "name":"my name1"}, {"empoyee_id":2, "name":"my name2"}], "d_employee_syntax": "release 17;\r\n datawindow(units=0 timer_interval=0 color=1073741824..." }
If the root of the JSON string is an array (not an object), as shown below, it is NOT supported by JSONPackage (but supported by JSONParser):
[{"id":1, "name":"evan1", "birthday":2340323884}]
You can change the JSON string like below, so it can be supported by JSONPackage:
{"id":1, "name":"evan1", "birthday":2340323884}
or
{"data":[{"id":1, "name":"evan1", "birthday":2340323884}]}
Note
To make sure your JSON file or JSON string is in the standard JSON format, you can use any third-party tool (such as the online JSON tool) to validate the JSON format.
Syntax
objectname.LoadFile ( FileName )
Argument |
Description |
---|---|
objectname |
The name of the JSONParser or JSONPackage object to which the JSON file will be loaded. |
FileName |
A string whose value is the file full name. Only the JSON or TXT file type is supported. If the function is called by JSONParser, the file content must be JSON formatted. If the function is called by JSONPackage, the file content must be JSON data of JsonObjectItem type. |
Return value
String.
Returns the empty string ("") if it succeeds and the error message if an error occurs. If any argument's value is null, the method returns null.
Usage
If a file is already loaded by this function, calling this function again will clean up the original file and then load the new file.
After the JSON file is loaded, you can get the data through various methods. Refer to the code examples in LoadString.
Examples
This example loads a JSON file into the JSONParser object:
String ls_Error JsonParser lnv_JsonParser lnv_JsonParser = Create JsonParser ls_Error = lnv_JsonParser.LoadFile("c:\employee.json") if Len(ls_Error) > 0 then MessageBox("Error", ls_Error) end if
This example loads data from the employees.txt file and then loads the "d_employee" data into the DataWindow:
string ls_EmployeeJson, ls_Error JsonPackage lnv_package lnv_package = create JsonPackage ls_Error = lnv_package.LoadFile ("d:\temp\employees.txt") if Len(ls_Error) = 0 then ls_EmployeeJson = lnv_package.GetValue("d_employee") dw_1.ImportJson(ls_EmployeeJson) else Messagebox("Error", ls_Error) end if
See also