Insert (Json function): Difference between revisions
Jump to navigation
Jump to search
(Automatically generated page update) |
No edit summary |
||
Line 1: | Line 1: | ||
{{Template:Json:Insert subtitle}} | {{Template:Json:Insert subtitle}} | ||
This function inserts an item into a Json array. | |||
This | |||
==Syntax== | ==Syntax== | ||
{{Template:Json:Insert syntax}} | {{Template:Json:Insert syntax}} | ||
===Syntax terms=== | ===Syntax terms=== | ||
<table class="syntaxTable"> | <table class="syntaxTable"> | ||
<tr><th>%number</th><td>number</td></tr> | <tr><th>%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 | <td>Json object, which must be an array or an InvalidJsonType exception is thrown.</td></tr> | ||
<tr><th>number</th> | <tr><th>number</th> | ||
<td>number</td></tr> | <td>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.</td></tr> | ||
<tr><th>json</th> | <tr><th>json</th> | ||
<td><var>Json</var> object</td></tr> | <td><var>Json</var> object that can be <var>Null</var>.</td></tr> | ||
</table> | </table> | ||
==Usage notes== | ==Usage notes== | ||
<ul> | |||
<li>The first parameter can also be thought of as the intended item number for the item being added.</li> | |||
<li>The value returned by Insert is the same value that would be returned by an immediately following [[Count (Json property)|Count function]].</li> | |||
<li>Unlike the [[Insert (Arraylist function)|Arraylist]] and [[Insert (Stringlist function)|Stringlist]] Insert methods, only one item can be added with the Json Insert function.</li> | |||
<li>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. | |||
</ul> | |||
==Examples== | ==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: | |||
<p class="code">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 | |||
</p> | |||
It prints: | |||
<p class="code">%json=["Start",2,3,5,7,11,13,17,19,23,29,"End"] | |||
</p> | |||
Of course, the insert of "End" above would be done much more sensibly with the [[Add (Json function)|Add function]]. | |||
==See also== | ==See also== | ||
{{Template:Json:Insert footer}} | {{Template:Json:Insert footer}} |
Revision as of 03:35, 18 February 2015
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
%number | The 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.