GetItemByPath

Description

Gets the handle of the item.

Applies to

JSONParser objects and JSONGenerator objects

Syntax

objectname.GetItemByPath ( ItemPath )

Argument

Description

objectname

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

ItemPath

A string specifying the path of the item. 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 "/".


Return value

Long.

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

Example 1

This example gets the item handle from a JSONParser object according to the item path from a one-dimensional 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":[55,22,33]}}'

lnv_JsonParser.LoadString(ls_Json)
ls_Path = "/name"
ll_item = lnv_JsonParser.GetItemByPath(ls_Path)
ls_Name = lnv_JsonParser.GetItemString(ll_item)
ls_Path = "/data_object/datetime"
ll_item = lnv_JsonParser.GetItemByPath(ls_Path)
ldt_DateTime = lnv_JsonParser.GetItemDateTime(ll_item)

Example 2

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 3

This example determines the item handle in a JSONGenerator object according to the item path and then adds three child items:

Long ll_ChildObject
String ls_RootPath,ls_ChildPath
JsonGenerator lnv_JsonGenerator
lnv_JsonGenerator = Create JsonGenerator

// Creates an object root item
lnv_JsonGenerator.CreateJsonObject ()

// Adds an object child item
ls_RootPath = "/"
lnv_JsonGenerator.AddItemObject(ls_RootPath, "object")
ls_ChildPath = "/object"
ll_ChildObject = lnv_JsonGenerator.GetItemByPath(ls_ChildPath)

lnv_JsonGenerator.AddItemNumber(ll_ChildObject, "year", 2017)
lnv_JsonGenerator.AddItemDate(ll_ChildObject, "date", 2017-09-21)
lnv_JsonGenerator.AddItemTime(ll_ChildObject, "time", 12:00:00)

See also

GetItemArray

GetItemArrayJSONString

GetItemBlob

GetItemBoolean

GetItemDate

GetItemDateTime

GetItemNumber

GetItemObject

GetItemObjectJSONString

GetItemString

GetItemTime

GetItemType

GetNumberType

GetPathByItem (JSONGenerator)