Sets a member within a JSON object or element within a JSON array to the given JSON Entity.

Syntax

Result = SRP_Json(Handle, "Set", Identifier, NewEntityHandle)

Returns

Returns 1 if successful, 0 if not.

Parameters

ParameterDescription
HandleHandle to a JSON Entity. Required.
IdentifierThe object member or array element to set. Required.
NewEntityHandleHandle to another JSON Entity. Optional.

Remarks

The Setservice sets a child within a given JSON Entity. This service only works on JSON objects and JSON arrays. If it is a JSON object, then Identifier needs to be a member name, such as "id" or "first-name". If it is a JSON array, then identifier needs to be an index, such as 1 or 10. The NewEntityHandle parameter must be a different valid JSON Entity Handle. The entity associated with that handle will be placed into the specified member/element of the entity associated with the Handle parameter.

It does not matter if the member/element already exists or not. Calling this service will either create the member/element if necessary or simply replace what's already there. For arrays, it does not matter how big the array currently is as the array will always grow in order to include the desired index. For example, if you have an array with 10 elements and then set element 25 to another entity, then the array will become 25 in size with elements 11-24 being defaulted to null.

Even though you are putting one entity into another, you still must Release the child handle when you no longer need it.

Example

// Create a simple string entity
If SRP_Json(StringHandle, "New", "String", "Hello, World!") then

   // Create a JSON array, set elements in any order
   If SRP_Json(ArrayHandle, "New", "Array") then
       SRP_Json(ArrayHandle, "Set", 1, StringHandle)
       SRP_Json(ArrayHandle, "Set", 10, StringHandle)
       SRP_Json(ArrayHandle, "Set", 5, StringHandle)
       SRP_Json(ArrayHandle, "Release")
   end
   
   // Create a JSON object, add members to it
   If SRP_Json(ObjectHandle, "New", "Object") then
       SRP_Json(ObjectHandle, "Set", "first-member", StringHandle)
       SRP_Json(ObjectHandle, "Set", "second-member", StringHandle)
       SRP_Json(ObjectHandle, "Release")
   end
   
   // Remember to release handles we created
   SRP_Json(StringHandle, "Release")
   
end

See Also

SetValueAddAddValue

  • No labels