Insert (Json function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
No edit summary
 
Line 5: Line 5:
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%number</th><td>The number of items in the array after the Add.</td></tr>
<tr><th nowrap>%number</th><td>The number of items in the array after the Add.</td></tr>
<tr><th>Json</th>
<tr><th>Json</th>
<td>Json object, which must be an array or an InvalidJsonType exception is thrown.</td></tr>
<td>Json object, which must be an array or an InvalidJsonType exception is thrown.</td></tr>
Line 13: Line 13:
<td><var>Json</var> object that can be <var>Null</var>.</td></tr>
<td><var>Json</var> object that can be <var>Null</var>.</td></tr>
</table>
</table>
==Usage notes==
==Usage notes==
<ul>
<ul>

Latest revision as of 15:51, 23 August 2016

Insert an item into a JSON array (Json class)

[Introduced in Model 204 7.6]

This function inserts an item into a Json array.

Syntax

[%number =] json:Insert( number, 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.
number The number of the item before which the new item is to be inserted. If this is one more than the number of items in the array, the item is added to the end of the array.
json Json object that can be Null.

Usage notes

  • The first parameter can also be thought of as the intended item number for the item being added.
  • The value returned by Insert is the same value that would be returned by an immediately following Count function.
  • Unlike the Arraylist and Stringlist Insert methods, only one item can be added with the Json Insert function.
  • Inserting an item into the middle of an array shifts subsequent items up. This means that subsequent items after an insert are accessed by a number one greater than before.

Examples

The following example populates an array with the first 10 prime numbers and then inserts the string "Start" at the beginning and "End" at the end:

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

It prints:

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

Of course, the insert of "End" above would be done much more sensibly with the Add function.

See also