Add (Json function)

From m204wiki
Revision as of 20:37, 17 February 2015 by Alex (talk | contribs) (→‎Examples)
Jump to navigation Jump to search

Add an item to a JSON array (Json class)

[Introduced in Model 204 7.6]


This page is under construction.

Syntax

[%number =] json:Add( json) Throws InvalidJsonType

Syntax terms

%numberThe number of items in the array after the Add.
Json Json object, which must be an array or an InvalidJsonType exception is thrown.
json Json object that can be Null.

Usage notes

  • The added Json object becomes the last item in the array.
  • The value returned by Add is the same value that would be returned by an immediately following Count function.
  • Unlike the Arraylist and Stringlist Add methods, only one item can be added with the Json Add function.

Examples

The following request adds the string "Start", the first 10 prime numbers, and then the string "End" to a Json object that is an array. This illustrates how different Json object types can be added to the same array. In fact, it would also possible to add a Boolean, Object, or even another Array type to the array.

b %i float %json is object json %prime is float %json = array %json:add("Start") for %i from 1 to 10 %prime = %prime:nextPrime %json:add(%prime) end for %json:add("End") printtext {~=%json} end

This program prints:

%json=["Start",2,3,5,7,11,13,17,19,23,29,"End"]

See also