ItemByNumber (Json function): Difference between revisions
(Automatically generated page update) |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{Template:Json:ItemByNumber subtitle}} | {{Template:Json:ItemByNumber subtitle}} | ||
This function returns a Json array or object item using the item number. | |||
This | |||
==Syntax== | ==Syntax== | ||
{{Template:Json:ItemByNumber syntax}} | {{Template:Json:ItemByNumber syntax}} | ||
Line 8: | Line 7: | ||
<tr><th>%json</th><td><var>Json</var> object</td></tr> | <tr><th>%json</th><td><var>Json</var> object</td></tr> | ||
<tr><th>Json</th> | <tr><th>Json</th> | ||
<td>Json object, which | <td>Json object, which must be either an array or object. If a null or other underlying type is used, an InvalidJsonType exception is thrown.</td></tr> | ||
<tr><th>number</th> | <tr><th>number</th> | ||
<td>number</td></tr> | <td>The item number of the object to return.</td></tr> | ||
</table> | </table> | ||
==Usage notes== | ==Usage notes== | ||
<ul> | |||
<li>For array retrievals, there is no difference between ItemByNumber and [Item (Json property)|Item]] so it probably makes more sense to use Item if the Json object is known to be an array. The ItemByNumber function is allowed to operate on arrays so that the same code could be used on both arrays and objects.</li> | |||
<li>The item number must be between 1 and the number of items (inclusive) in the array or object ([[Count (Json property)|Count]]). An invalid item number results in request cancellation.</li> | |||
<li>For objects, the names of the items can be retrieved by number using the [[NameByNumber (Json function)|NameByNumber function]].</li> | |||
</ul> | |||
==Examples== | ==Examples== | ||
The following example populates an object where each item is an array and then displays the name and value of each of the object items using NameByNumber and ItemByNumber: | |||
<p class="code">b | |||
%json is object json | |||
%i is float | |||
%json = object | |||
%json('Packers') = array(1967, 1968, 1997, 2011) | |||
%json('Steelers') = array(1975, 1976, 1979, 1980, 2006, 2009) | |||
%json('Cowboys') = array(1972, 1978, 1993, 1994, 1996, 2009) | |||
%json('49ers') = array(1982, 1985, 1989, 1990, 1995) | |||
%json('Patriots') = array(2002, 2004, 2005, 2015) | |||
for %i from 1 to %json:count | |||
printText {%json:nameByNumber(%i)}: {%json:itemByNumber(%i)} | |||
end for | |||
end | |||
</p> | |||
This prints: | |||
<p class="code">Packers: [1967,1968,1997,2011] | |||
Steelers: [1975,1976,1979,1980,2006,2009] | |||
Cowboys: [1972,1978,1993,1994,1996,2009] | |||
49ers: [1982,1985,1989,1990,1995] | |||
Patriots: [2002,2004,2005,2015] | |||
</p> | |||
==See also== | ==See also== | ||
{{Template:Json:ItemByNumber footer}} | {{Template:Json:ItemByNumber footer}} |
Latest revision as of 15:53, 18 February 2015
Return JSON object/array item value by item number (Json class)
[Introduced in Model 204 7.6]
This function returns a Json array or object item using the item number.
Syntax
%outJson = json:ItemByNumber( number) Throws InvalidJsonType
Syntax terms
%json | Json object |
---|---|
Json | Json object, which must be either an array or object. If a null or other underlying type is used, an InvalidJsonType exception is thrown. |
number | The item number of the object to return. |
Usage notes
- For array retrievals, there is no difference between ItemByNumber and [Item (Json property)|Item]] so it probably makes more sense to use Item if the Json object is known to be an array. The ItemByNumber function is allowed to operate on arrays so that the same code could be used on both arrays and objects.
- The item number must be between 1 and the number of items (inclusive) in the array or object (Count). An invalid item number results in request cancellation.
- For objects, the names of the items can be retrieved by number using the NameByNumber function.
Examples
The following example populates an object where each item is an array and then displays the name and value of each of the object items using NameByNumber and ItemByNumber:
b %json is object json %i is float %json = object %json('Packers') = array(1967, 1968, 1997, 2011) %json('Steelers') = array(1975, 1976, 1979, 1980, 2006, 2009) %json('Cowboys') = array(1972, 1978, 1993, 1994, 1996, 2009) %json('49ers') = array(1982, 1985, 1989, 1990, 1995) %json('Patriots') = array(2002, 2004, 2005, 2015) for %i from 1 to %json:count printText {%json:nameByNumber(%i)}: {%json:itemByNumber(%i)} end for end
This prints:
Packers: [1967,1968,1997,2011] Steelers: [1975,1976,1979,1980,2006,2009] Cowboys: [1972,1978,1993,1994,1996,2009] 49ers: [1982,1985,1989,1990,1995] Patriots: [2002,2004,2005,2015]