Description
Gets the type of the item.
Applies to
Syntax for JSONParser
objectname.GetItemType ( ItemHandle )
objectname.GetItemType ( ParentItemHandle, Key )
objectname.GetItemType ( ItemPath )
Syntax for JSONPackage
objectname.GetItemType ( Key )
Argument |
Description |
---|---|
objectname |
The name of the JSONParser or JSONPackage object whose item type you want to obtain. |
ItemHandle |
A long whose value is the item handle. |
ParentItemHandle |
A long specifying the parent item handle which is JsonObjectItem type. |
ItemPath |
A string specifying the item path. If there is a multi-dimensional array, use the number to indicate the order of the array elements. If a key name contains "/", use the escape character "~~/" to replace "/". |
Key |
A string specifying the key of the child item. |
Return value
JsonItemType.
Returns the JsonItemType enumerated value if it succeeds and null value if an error occurs. If any argument's value is null, the method returns null.
The JsonItemType enumerated values are:
-
JsonStringItem! -- Type of the JSON node whose key value pair is a string, such as "name":"evan".
-
JsonNumberItem! -- Type of the JSON node whose key value pair is a number, such as "id":1001.
-
JsonBooleanItem! -- Type of the JSON node whose key value pair is a boolean, such as "active":true.
-
JsonNullItem! -- Type of the JSON node whose key value pair is null, such as "remark": null.
-
JsonObjectItem! -- Type of the JSON node whose key value pair is an object, such as "date_object":{"datetime":7234930293, "date": "2017-09-21", "time": "12:00:00"}.
-
JsonArrayItem! -- Type of the JSON node whose key value pair is an array, such as "department_array":[999999, {"name":"Website"}, {"name":"PowerBuilder"}, {"name":"IT"}].
Example 1
This example gets the key value and type of the child items according to the item handle in a loop:
JsonParser lnv_JsonParser String ls_Json, ls_key, ls_value Long ll_RootObject, ll_item, ll_ChildCount, ll_index lnv_JsonParser = Create JsonParser ls_Json = '{"id":1001, "name":"svan", "active":true}' // Loads a JSON string lnv_JsonParser.LoadString(ls_Json) ll_RootObject = lnv_JsonParser.GetRootItem() // Gets the key value and type of the child items ll_ChildCount = lnv_JsonParser.GetChildCount(ll_RootObject) for ll_index = 1 to ll_ChildCount ls_key = lnv_JsonParser.GetChildKey(ll_RootObject, ll_index) ll_item = lnv_JsonParser.GetChildItem(ll_RootObject, ll_index) choose case lnv_JsonParser.GetItemType(ll_item) case JsonStringItem! ls_value = lnv_JsonParser.GetItemString(ll_RootObject, ls_key) case JsonNumberItem! ls_value = string(lnv_JsonParser.GetItemNumber(ll_RootObject, ls_key)) case JsonBooleanItem! ls_value = string(lnv_JsonParser.GetItemBoolean(ll_RootObject, ls_key)) case JsonNullItem! ls_value = 'Null' end choose MessageBox("Info", String(ll_index) + ': key = ' + ls_key + ', value = ' + ls_value) next
Example 2
This example gets the key value and type of the child items according to the parent item handle in a recursive traversal:
String ls_Json Long ll_RootItem JsonParser lnv_JsonParser lnv_JsonParser = Create JsonParser ls_Json = '{"id":1001, "name":"evan", "active":true, "department_array":[999999, {"name":"Website"}, {"name":"PowerBuilder"}, {"name":"IT"}]}' // Loads a string lnv_JsonParser.LoadString(ls_Json) // Obtains root item (type is JsonObjectItem! or JsonArrayItem!) Long ll_RootItem = lnv_JsonParser.GetRootItem() // Gets each item recursively (this is a recursive function) of_ParseJson(lnv_JsonParser, ll_RootItem) // *****************Traverse each item recursively***************************** public function integer of_parsejson (jsonparser anv_jsonparser, long alp_handle); long ll_Index, ll_ChildCount long ll_Child string ls_Null, ls_value double ldb_value boolean lb_value JsonItemType ljit_JsonItemType // Obtains item type ljit_JsonItemType = anv_JsonParser.GetItemType(alp_Handle) // Gets item recursively or gets value according to the type if ljit_JsonItemType = JsonObjectItem! or ljit_JsonItemType = JsonArrayItem! then // Gets item recursively ll_ChildCount = anv_JsonParser.GetChildCount(alp_Handle) for ll_Index = 1 to ll_ChildCount ll_Child = anv_JsonParser.GetChildItem(alp_Handle, ll_Index) of_ParseJson(anv_JsonParser, ll_Child) next else // Gets value choose case ljit_JsonItemType case JsonStringItem! ls_value = anv_JsonParser.GetItemString(alp_Handle) case JsonNumberItem! ldb_value = anv_JsonParser.GetItemNumber(alp_Handle) case JsonBooleanItem! lb_value = anv_JsonParser.GetItemBoolean(alp_Handle) case JsonNullItem! SetNull(ls_Null) end choose end if return 1 end function
Example 3
This example gets the key value and data type of the child items according to the item path:
JsonParser lnv_JsonParser String ls_Json, ls_key, ls_value, ls_RootPath, ls_ChildPath Long ll_ChildCount, ll_index lnv_JsonParser = Create JsonParser ls_Json = '{"id":1001, "name":"svan", "active":true}' // Loads a JSON string lnv_JsonParser.LoadString(ls_Json) ls_RootPath = "/" // Gets the key value and type of the child items ll_ChildCount = lnv_JsonParser.GetChildCount(ls_RootPath) for ll_index = 1 to ll_ChildCount ls_key = lnv_JsonParser.GetChildKey(ls_RootPath, ll_index) ls_ChildPath = ls_RootPath + ls_Key choose case lnv_JsonParser.GetItemType(ls_ChildPath) case JsonStringItem! ls_value = lnv_JsonParser.GetItemString(ls_ChildPath) case JsonNumberItem! ls_value = string(lnv_JsonParser.GetItemNumber(ls_ChildPath)) case JsonBooleanItem! ls_value = string(lnv_JsonParser.GetItemBoolean(ls_ChildPath)) case JsonNullItem! ls_value = 'Null' end choose MessageBox("Info", String(ll_index) + ': key = ' + ls_key + ', value = ' + ls_value) next
Example 4
This example gets the data type of the child items according to the specified index and then gets the key value according to the type:
// JsonItemType GetItemType ( string Key ) String ls_KeyValue String ls_KeyName JsonPackage ljpk_Dept ljpk_Dept = Create JsonPackage // Loads the JSON string to the JSONPackage object ljpk_Dept.loadstring( '{"dept_id":100, "dept_name":"R & D8", "Status":true, "array":[{"dept_name":"R & D8"}, {"dept_name":"Sales"}, {"dept_name":"Finance"}]}') ls_KeyName = ljpk_Dept.GetKey( 4 ) //The index of the key in the JSON string Choose Case ljpk_Dept.GetItemType(ls_KeyName) Case JsonStringItem! ls_KeyValue = ljpk_Dept.getvalueString( ls_KeyName ) Case JsonNumberItem! ls_KeyValue = String (ljpk_Dept.getvalueNumber( ls_KeyName )) Case JsonBooleanItem! ls_KeyValue = String (ljpk_Dept.GetValueBoolean( ls_KeyName )) Case JsonNullItem! SetNull(ls_KeyValue) Case JsonObjectItem!,JsonArrayItem! ls_KeyValue = ljpk_Dept.GetValue( ls_KeyName ) Case Else // Prints message for unknown item type End Choose // Prints key value index 4 output: [{"dept_name":"R & D8"},{"dept_name":"Sales"},{"dept_name":"Finance"}]
Example 5
This example loads a JSON string to a JSONParser object and gets and processes the value according to the type.
Integer i Long ll_RootHandle Long ll_Objectc double ldb_Value Boolean lb_Value Long ll_Object String ls_Return String ls_Value String ls_Key JSONItemType ljit_Information JsonParser ljp_Dept ljp_Dept = Create JsonParser // Loads the JSON string to the JSONParser object ls_Return = ljp_Dept.LoadString ( '{"Name":"Ann.Mo", "Boolean":false, "address":{"city":"shezhen"}, "dept":[{"dept_id":100, "dept_name":"R & D8", "dept_head_id":105}, {"dept_id":200, "dept_name":"Sales", "dept_head_id":129}]}' ) If Trim(ls_Return)<>"" Then // Prints error message Return End If ll_RootHandle = ljp_Dept.GetRootItem( ) For i = 1 To ljp_Dept.GetChildCount( ll_RootHandle ) ls_Key = ljp_Dept.GetChildKey( ll_RootHandle, i ) // Gets and processes the value according to the type Choose Case ljp_Dept.GetItemType( ll_RootHandle, ls_Key ) Case JsonStringItem! ls_Value = ljp_Dept.GetItemString(ll_RootHandle, ls_Key ) Case JsonNumberItem! ldb_Value = ljp_Dept.GetItemNumber( ll_RootHandle, ls_Key ) Case JsonBooleanItem! lb_Value = ljp_Dept.GetItemBoolean( ll_RootHandle, ls_Key ) Case JsonNullItem! ls_Value = "Null" Case JsonObjectItem! ll_Object = ljp_Dept.GetItemObject( ll_RootHandle, ljp_Dept.GetChildKey( ll_RootHandle, i ) ) Case JsonArrayItem! ll_Object = ljp_Dept.GetItemArray( ll_RootHandle, ljp_Dept.GetChildKey( ll_RootHandle, i ) ) Case Else // Prints error message End Choose Next
See also