Description
Loads a JSON string to the JSONParser or JSONPackage objects.
Applies to
Required JSON format
-
For JSONParser object: The string that can be loaded by the JSONParser object must be JSON-formatted.
-
For JSONPackage object: The string 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.LoadString ( JsonData )
Argument |
Description |
---|---|
objectname |
The name of the JSONParser or JSONPackage object to which the JSON string will be loaded. |
JsonData |
(For JSONParser object) A JSON-formatted string. (For JSONPackage object) The 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
When this function is called, it will clear all JSON data in JSONParser/JSONPackage before loading the string.
After the JSON string is loaded, you can get the data through the following methods:
-
gets an array according to its item path. See below Example 3.
-
gets the JSON string of an array according to its item handle. See below Example 4.
-
gets the item handle from a JSONParser object according to the item path from a two-dimensional array. See below Example 5.
-
gets the total number of child items according to the parent item handle and then gets the value of every array item in a loop. See below Example 6.
-
determines the type of number values according to the item handle. See below Example 7.
-
handles an irregular JSON string which contains a null value. See below Example 8.
Examples
Example 1: This example loads a JSON string into the JSONParser object.
String ls_Error JsonParser lnv_JsonParser lnv_JsonParser = Create JsonParser String ls_Json = '{"id":1, "name":"evan1", "birthday":2340323884}' ls_Error = lnv_JsonParser.LoadString(ls_Json) if Len(ls_Error) > 0 then MessageBox("Error", ls_Error) end if
Example 2: This example gets the data from the server and then loads the "d_employee" data to the DataWindow.
int li_rc string ls_JsonPackage, ls_Error, ls_EmployeeJson HttpClient lnv_HttpClient JsonPackage lnv_package lnv_HttpClient = create HttpClient lnv_package = create JsonPackage // Request the JSON data package from server li_rc = lnv_HttpClient.SendRequest("GET", "https://demo.appeon.com/PB/webapi_client/getjsonpackage/employee/102") // Get the data if li_rc = 1 and lnv_HttpClient.GetResponseStatusCode() = 200 then lnv_HttpClient.GetResponseBody(ls_JsonPackage) // Extract the JSON data package ls_Error = lnv_package.LoadString(ls_JsonPackage) 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 end if
Example 3: The following code gets an array according to its item path.
JsonParser lnv_JsonParser String ls_Json, ls_name, ls_Path Long ll_number_item, ll_number, ll_object_item Long ll_department_array lnv_JsonParser = Create JsonParser ls_Json = '{"id":1001, "name":"evan", "department_array":[999999, {"name":"Website"}, {"name":"PowerBuilder"}, {"name":"IT"}] }' lnv_JsonParser.LoadString(ls_Json) ls_Path = "/department_array" ll_department_array = lnv_JsonParser.GetItemArray(ls_Path) ll_number_item = lnv_JsonParser.GetChildItem(ll_department_array, 1) ll_number = lnv_JsonParser.GetItemNumber(ll_number_item) ll_object_item = lnv_JsonParser.GetChildItem(ll_department_array, 2) ls_name = lnv_JsonParser.GetItemString(ll_object_item, "name")
Example 4: The following code gets the JSON string of an array according to its item handle.
JsonParser lnv_JsonParser String ls_Json, ls_Array Long ll_RootObject, ll_ChildArray, ll_Handle lnv_JsonParser = Create JsonParser ls_Json = '{"id":1001, "name":"svan", "active":true,"array":[12,34,[56,78]]}' lnv_JsonParser.LoadString(ls_Json) ll_RootObject = lnv_JsonParser.GetRootItem() ll_ChildArray = lnv_JsonParser.GetItemArray(ll_RootObject,"array") ll_Handle = lnv_JsonParser.GetChildItem(ll_ChildArray, 3) ls_Array = lnv_JsonParser.GetItemArrayJSONString(ll_Handle)
Example 5: This example gets the item handle from a JSONParser object according to the item path from a two-dimensional array. The number indicates the order of the array.
JsonParser lnv_JsonParser String ls_Json, ls_Name, ls_Path DateTime ldt_DateTime Long ll_item lnv_JsonParser = Create JsonParser ls_Json = '[{"id":1001, "name":"evan", "data_object":{"datetime":7234930293, "date": "2017-09-21", "time": "12:00:00","age":[66,22,33]}},' + & '{"id":1002, "name":"evan2", "data_object":{"datetime":1734930293, "date": "2017-09-11", "time": "11:00:00","age":[55,23,33]}}]' lnv_JsonParser.LoadString(ls_Json) ls_Path = "/1/name" ll_item = lnv_JsonParser.GetItemByPath(ls_Path) ls_Name = lnv_JsonParser.GetItemString(ll_item) ls_Path = "/2/data_object/datetime" ll_item = lnv_JsonParser.GetItemByPath(ls_Path) ldt_DateTime = lnv_JsonParser.GetItemDateTime(ll_item)
Example 6: This example gets the total number of child items according to the parent item handle and then gets the value of every array item in a loop.
String ls_Json, ls_Name Long ll_ChildCount, ll_Index, ll_Id, ll_ArrayItem, ll_ObjectItem Datetime ldt_Birthday JsonParser lnv_JsonParser lnv_JsonParser = Create JsonParser ls_Json = '[{"id":1, "name":"evan1", "birthday":2340323884}, {"id":2, "name":"evan2", "birthday":5340324801}]' // Loads a JSON string lnv_JsonParser.LoadString(ls_Json) Long ll_ArrayItem = lnv_JsonParser.GetRootItem() // Root item is JsonArrayItem! ll_ChildCount = lnv_JsonParser.GetChildCount(ll_ArrayItem) // Gets the array item in a loop for ll_Index = 1 to ll_ChildCount // Gets the array item Long ll_ObjectItem = lnv_JsonParser.GetChildItem(ll_ArrayItem, ll_Index) // Array item is JsonObjectItem! if lnv_JsonParser.GetItemType(ll_ObjectItem) = JsonObjectItem! then ll_Id = lnv_JsonParser.GetItemNumber(ll_ObjectItem, "id") ls_Name = lnv_JsonParser.GetItemString(ll_ObjectItem, "name") ldt_Birthday = lnv_JsonParser.GetItemDateTime(ll_ObjectItem, "birthday") end if ... next
Example 7: This example determines the type of number values according to the item handle.
Long ll_ItemCount, ll_I, ll_RootItem, ll_Child String ls_Json, ls_Return, ls_Key, ls_Value Dec ldc_Value JsonItemType ljs_type, ljs_Root JsonNumberType ljsn_type JsonParser ljs_par ljs_par = Create JsonParser ls_Json = '{"value1":123.45,"value2":Infinity,"value3":-Infinity,"value4":NaN,"value5":null}' ls_Return = ljs_par.LoadString(ls_Json) If Len(ls_Return) > 0 Then Return ll_RootItem = ljs_par.GetRootItem() ll_ItemCount = ljs_Par.GetChildCount(ll_RootItem) ljs_Root = ljs_par.GetItemType(ll_RootItem) For ll_I = 1 To ll_ItemCount ll_child = ljs_par.getchilditem( ll_RootItem, ll_i) ls_Key = ljs_par.GetChildKey(ll_RootItem, ll_i) ljs_type = ljs_par.GetItemType(ll_child) Choose Case ljs_type Case Jsonnumberitem! ldc_Value = ljs_par.GetItemNumber(ll_child) If IsNull ( ldc_value ) Then ljsn_type = ljs_par.GetNumberType(ll_child) Choose Case ljsn_type Case JsonNaN! ls_value = "Nan" Case JsonPositiveInfinity! ls_value = "Infinity" Case JsonNegativeInfinity! ls_value = "-Infinity" Case JsonNumber! ls_value = "null" Case Else End Choose Else ls_value = String(ldc_value) End If Case Jsonnullitem! ls_value = "null" End Choose ls_Return += ls_Key + "=" + ls_Value + "~r~n" Next If IsValid ( ljs_par ) Then Destroy ( ljs_par )
Example 8: The following code handles an irregular JSON string which contains a null value.
long ll_loop,i long ll_row long ll_root,ll_object,ll_item //receiving the handle of JSON item string ls_json,ls_error string ls_return string ls_key jsonparser lnv_jsonparser lnv_jsonparser = create jsonparser ls_json = "[{~"ID~":101,~"FirstName~":~"Li~"},{~"ID~":102,~"FirstName~":null}]" // JSON data contains a null value //Loads the JSON data ls_error= lnv_jsonparser.loadstring(ls_json) if len(trim(ls_error)) > 0 then messagebox("Failed","load json failed:"+ls_error) return end if //Obtains the handle of root item ll_root = lnv_jsonparser.getrootitem() //Obtains the data of each row for ll_loop = 1 to lnv_jsonparser.getchildcount(ll_root) //Obtains the handle of each row ll_object = lnv_jsonparser.getchilditem(ll_root,ll_loop) //Inserts a row into datawindow ll_row = dw_1.insertrow(0) //Parses the item value one by one in a row in a loop for i = 1 to lnv_jsonparser.getchildcount(ll_object) //Obtains the handle and key of each item ll_item = lnv_jsonparser.getchilditem(ll_object,i) ls_key = lnv_jsonparser.getchildkey(ll_object,i) //Checks the data type of each item choose case lnv_jsonparser.getitemtype(ll_item) case jsonarrayitem!,jsonobjectitem! messagebox("Error","Not standard datatype") //Item value cannot be inserted to datawindow case jsonnumberitem! //Obtains number data dw_1.setitem(ll_row,i,lnv_jsonparser.getitemnumber(ll_item)) //dw_1.setitem(ll_row,ls_key,lnv_jsonparser.getitemnumber(ll_object,ls_key)) or set data by column name case jsonstringitem! //Obtains string data dw_1.setitem(ll_row,i,lnv_jsonparser.getitemstring(ll_item)) case jsonbooleanitem! //Obtains boolean data. boolean converted to string and inserted to datawindow dw_1.setitem(ll_row,i,string(lnv_jsonparser.getitemboolean(ll_item))) case jsonnullitem! //null value. Not inserted to datawindow. end choose next //Finish parsing one row next//Start parsing next row
See also