Item (Json property): Difference between revisions

From m204wiki
Jump to navigation Jump to search
(Automatically generated page update)
 
No edit summary
Line 1: Line 1:
{{Template:Json:Item subtitle}}
{{Template:Json:Item subtitle}}
 
This function retrieves of sets an item in a Json array or object, using either the item number for an array or the item (property) name for an object.
This page is [[under construction]].
==Syntax==
==Syntax==
{{Template:Json:Item syntax}}
{{Template:Json:Item syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%json</th><td><var>Json</var> object</td></tr>
<tr><th>%currentJson</th><td><var>Json</var> object being retrieved from the object or array.</td></tr>
<tr><th>Json</th>
<tr><th>Json</th>
<td>Json object, which may be <var>Null</var></td></tr>
<td><var>Json</var> object, which must be either an object or array.</td></tr>
<tr><th>nonObject</th>
<tr><th>index</th>
<td>NonObject</td></tr>
<td>The number of the item to set or retrieve if the Json object is an array or the name of the item (property) to set or retrieve if the Json object is an object.</td></tr>
<tr><th>newJson</th><td><var>Json</var> object being added to the object or array.</td></tr>
</table>
</table>
==Usage notes==
==Usage notes==
<ul>
<li>As with the [[Item (Arraylist property)|Arraylist Item property]] and the [[Item (Stringlist function)|Stringlist Item function]], the Item method can be invoked implicitly. That is <code>%json:item(%x)</code> is identical to <code>%json(%x)</code>.</li>
<li>JavaScript and JSON terminology refer to the <i>properties</i> of an object. However, the SOUL equivalent of JSON objects, [[NamedArraylist class|NamedArraylists]] use the term <i>items</i> so, since the Json class is a SOUL feature, generally object properties will be referred to as named items.</li> 
<li>If the underlying Json object is an object, the input index is treated as a string, regardless of its datatype.</li>
<li>The names used for objects are always stored internally as unicode so the names can be specified as unicode variables or constants.</li>
<li>If the underlying Json object is an object, specifying a name that is not in the object returns a null on a retrieval and adds the name on a set.</li>
<li>If the underlying Json object is an array, the index must specify an item number that is between 1 and the number of items in the array (inclusive) for a retrieval, and between 1 and the number of items in the array plus one (inclusive) for a set. If the item number specified on a set is one more than the number of items in the array, the item is added to the end of the array.</li>
<li>If the underlying Json object is an array, the input index is treated as a number, regardless of its datatype. This means that if it's a string that's not a valid number, it's converted to a 0 which is not a valid item number so results in request cancellation.</li>
</ul>
==Examples==
==Examples==
The following example sets some object items (properties) by name and then retrieves their values to use in an addition:
<p class="code">b
%json is object json
%json = object
%json('Paris')    = 2243833
%json('Marseille') = 850726
%json('Lyon')      = 484344
%json('Toulouse')  = 441802
printText {~=%json('Paris'):numberValue  + %json('Marseille'):numberValue}
printText {~=%json('Paris'):numberValue  + %json('Lyon'):numberValue}
printText {~=%json('Paris'):numberValue  + %json('Toulouse'):numberValue}
end
</p>
The above prints:
<p class="code">%json('Paris'):numberValue  + %json('Marseille'):numberValue=3094559
%json('Paris'):numberValue  + %json('Lyon'):numberValue=2728177
%json('Paris'):numberValue  + %json('Toulouse'):numberValue=2685635
</p>
The following example replaces the values of some Json array items and then retrieves each item for printing:
<p class="code">b
%json is object json
%i    is float
%json = array(2, 3, 5, 7, 11, 13, 17, 19)
%json(2) = "Berlin"
%json(5) = "Hamburg"
%json(6) = "Munich"
for %i from 1 to %json:count
  printText {%i}: {%json(%i)}
end for
end
</p>
This prints:
<p class="code">1: 2
2: "Berlin"
3: 5
4: 7
5: "Hamburg"
6: "Munich"
7: 17
8: 19
</p>
Of course, if the intent was just to see the contents of the array, the array itself could just be printed, though the format of the output would be slightly different.
==See also==
==See also==
{{Template:Json:Item footer}}
{{Template:Json:Item footer}}

Revision as of 14:44, 18 February 2015

Return or set JSON object/array item value (Json class)

[Introduced in Model 204 7.6]

This function retrieves of sets an item in a Json array or object, using either the item number for an array or the item (property) name for an object.

Syntax

%currentJson = json:Item( index) json:Item( index) = newJson Throws InvalidJsonType

Syntax terms

%currentJsonJson object being retrieved from the object or array.
Json Json object, which must be either an object or array.
index The number of the item to set or retrieve if the Json object is an array or the name of the item (property) to set or retrieve if the Json object is an object.
newJsonJson object being added to the object or array.

Usage notes

  • As with the Arraylist Item property and the Stringlist Item function, the Item method can be invoked implicitly. That is %json:item(%x) is identical to %json(%x).
  • JavaScript and JSON terminology refer to the properties of an object. However, the SOUL equivalent of JSON objects, NamedArraylists use the term items so, since the Json class is a SOUL feature, generally object properties will be referred to as named items.
  • If the underlying Json object is an object, the input index is treated as a string, regardless of its datatype.
  • The names used for objects are always stored internally as unicode so the names can be specified as unicode variables or constants.
  • If the underlying Json object is an object, specifying a name that is not in the object returns a null on a retrieval and adds the name on a set.
  • If the underlying Json object is an array, the index must specify an item number that is between 1 and the number of items in the array (inclusive) for a retrieval, and between 1 and the number of items in the array plus one (inclusive) for a set. If the item number specified on a set is one more than the number of items in the array, the item is added to the end of the array.
  • If the underlying Json object is an array, the input index is treated as a number, regardless of its datatype. This means that if it's a string that's not a valid number, it's converted to a 0 which is not a valid item number so results in request cancellation.

Examples

The following example sets some object items (properties) by name and then retrieves their values to use in an addition:

b %json is object json %json = object %json('Paris') = 2243833 %json('Marseille') = 850726 %json('Lyon') = 484344 %json('Toulouse') = 441802 printText {~=%json('Paris'):numberValue + %json('Marseille'):numberValue} printText {~=%json('Paris'):numberValue + %json('Lyon'):numberValue} printText {~=%json('Paris'):numberValue + %json('Toulouse'):numberValue} end

The above prints:

%json('Paris'):numberValue + %json('Marseille'):numberValue=3094559 %json('Paris'):numberValue + %json('Lyon'):numberValue=2728177 %json('Paris'):numberValue + %json('Toulouse'):numberValue=2685635

The following example replaces the values of some Json array items and then retrieves each item for printing:

b %json is object json %i is float %json = array(2, 3, 5, 7, 11, 13, 17, 19) %json(2) = "Berlin" %json(5) = "Hamburg" %json(6) = "Munich" for %i from 1 to %json:count printText {%i}: {%json(%i)} end for end

This prints:

1: 2 2: "Berlin" 3: 5 4: 7 5: "Hamburg" 6: "Munich" 7: 17 8: 19

Of course, if the intent was just to see the contents of the array, the array itself could just be printed, though the format of the output would be slightly different.

See also