The lookup
method returns the value at the specified path in the JSON value.
This method does not make a copy of the value, so modifying the returned value affects the original object.
lookup( pathElements )
Argument | Description |
---|---|
pathElements
|
(json_path_string_or_integer) The path of the value to return. Specify one or more path elements, which might be object attribute names (strings) or array indexes (integers). |
(LuaJsonValue) Returns the value that exists at the specified path, or nil
if the specified path does not exist.
local myJsonObject = LuaJsonObject:new() myJsonObject:assign( { attr1=LuaJsonObject:new( { n=42, x=5 } ), attr2=LuaJsonArray:new( 1, 2, "three" ), } ) local myJsonValue = LuaJsonValue:new( myJsonObject ) print ( myJsonValue:lookup("attr2",2):value() ) -- three (LuaJsonArrays are zero-indexed) -- Modifying the returned value changes the original object local lookup = myJsonValue:lookup("attr1"):object() lookup:assign("y",3) print (myJsonValue:string()) -- {"attr1":{"n":42,"x":5,"y":3},"attr2":[1,2,"three"]}
|