GetChildCount

Description

Gets the child item count in a JSON parser object.

Applies to

JSONParser objects

Syntax

objectname.GetChildCount ( ParentItemHandle )
objectname.GetChildCount ( ParentItemPath )

Argument

Description

objectname

The name of the JSONParser object whose item count you want to obtain.

ParentItemHandle

A long whose value is the handle of the parent item of JsonObjectItem or JsonArrayItem type.

ParentItemPath

A string whose value is the path of the parent item of JsonObjectItem or JsonArrayItem type. If a key name contains "/", use the escape character "~~/" to replace "/".


Return value

Long.

Returns the child item count if it succeeds and -1 if an error occurs. If any argument's value is null, the method returns null.

Example 1

This example gets the total number of child items according to the parent item handle and then gets the values of child items in a loop:

JsonParser lnv_JsonParser
Long ll_RootObject, ll_ChildCount, ll_Index
String ls_Json, ls_key
lnv_JsonParser = Create JsonParser

ls_Json = '{"id":1001, "name":"evan", "active":true}'

// Loads a JSON string
lnv_JsonParser.LoadString(ls_Json)
ll_RootObject = lnv_JsonParser.GetRootItem()

ll_ChildCount = lnv_JsonParser.GetChildCount(ll_RootObject)
// Obtains the values of child items
for ll_Index = 1 to ll_ChildCount
 ls_key = lnv_JsonParser.GetChildKey(ll_RootObject, ll_Index)
next

Example 2

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 3

This example gets the total number of child items according to the parent item path and then gets the value of every array item in a loop:

String ls_Json, ls_Name, ls_RootPath, ls_ArrayPath, ls_ChildPath
Long ll_ChildCount, ll_Index, ll_Id
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)
ls_RootPath = "/"
ll_ChildCount = lnv_JsonParser.GetChildCount(ls_RootPath)

// Gets the array item in a loop
for ll_Index = 1 to ll_ChildCount
  // Gets the array item
  ls_ArrayPath = ls_RootPath + String(ll_Index)
  // Array item is JsonObjectItem!
  if lnv_JsonParser.GetItemType(ls_ArrayPath) = JsonObjectItem! then
  ls_ChildPath = ls_ArrayPath + "/" + "id"
  ll_Id = lnv_JsonParser.GetItemNumber(ls_ChildPath)
  ls_ChildPath = ls_ArrayPath + "/" + "name"
  ls_Name = lnv_JsonParser.GetItemString(ls_ChildPath)
  ls_ChildPath = ls_ArrayPath + "/" + "birthday"
  ldt_Birthday = lnv_JsonParser.GetItemDateTime(ls_ChildPath)
  end if
  //...
next

See also

GetChildItem

GetChildKey