NumberByName (Json function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
No edit summary
 
(One intermediate revision by one other user not shown)
Line 5: Line 5:
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%number</th><td>The number of the item with the specified name. If no item has the name, a 0 is returned.</td></tr>
<tr><th nowrap>%number</th><td>The number of the item with the specified name. If no item has the name, a 0 is returned.</td></tr>
<tr><th>Json</th>
<tr><th>Json</th>
<td>Json object which must have an underlying type of object. A null or any other underlying type throws an InvalidjsonType exception.</td></tr>
<td>Json object which must have an underlying type of object. A null or any other underlying type throws an InvalidjsonType exception.</td></tr>
Line 11: Line 11:
<td>The name to be found. As <i>unicode</i> suggests, this name is treated as unicode since all names are stored internally as unicode.</td></tr>
<td>The name to be found. As <i>unicode</i> suggests, this name is treated as unicode since all names are stored internally as unicode.</td></tr>
</table>
</table>
==Usage notes==
==Usage notes==
<ul>
<ul>
Line 16: Line 17:
</ul>
</ul>
==Examples==
==Examples==
The following example populates an object with some values and then retrieves the values for names "Ostrich" and "Diplodocus". As you can see, the values are identical. However, the NumberByName method values for "Ostrich" and "Diplodocus" reveal the one is in the object but set to null while the other is simply not in the object.
The following example populates an object with some values and then retrieves the values for names "Ostrich" and "Diplodocus". As you can see, the values are identical. However, the NumberByName method values for "Ostrich" and "Diplodocus" reveal that one is in the object but set to null while the other is simply not in the object.
<p class="code">b
<p class="code">b


Line 41: Line 42:
%json:numberByName("Diplodocus")=3
%json:numberByName("Diplodocus")=3
</p>
</p>
==See also==
==See also==
{{Template:Json:NumberByName footer}}
{{Template:Json:NumberByName footer}}

Latest revision as of 15:56, 23 August 2016

Get number of named item in JSON object (Json class)

[Introduced in Model 204 7.6]

This function returns the item number associated with a particular name in a Json object with an underlying object type.

Syntax

%number = json:NumberByName( unicode)

Syntax terms

%numberThe number of the item with the specified name. If no item has the name, a 0 is returned.
Json Json object which must have an underlying type of object. A null or any other underlying type throws an InvalidjsonType exception.
unicode The name to be found. As unicode suggests, this name is treated as unicode since all names are stored internally as unicode.

Usage notes

  • Probably the main use of NumberByName is to determine if a given name is present in an object. Since the Item property will return a null if either the requested name is not present in the object or if the value associated with that name is null, NumberByName is one way to distinguish those cases.

Examples

The following example populates an object with some values and then retrieves the values for names "Ostrich" and "Diplodocus". As you can see, the values are identical. However, the NumberByName method values for "Ostrich" and "Diplodocus" reveal that one is in the object but set to null while the other is simply not in the object.

b %json is object json %json = object %json("Tiger") = "Asia" %json("Lion") = "Africa" %json("Diplodocus") = null %json("Elk") = array("North America", "Asia") %json("Kangaroo") = "Australia" printText {~=%json("Ostrich")} printText {~=%json("Diplodocus")} printText {~=%json:numberByName("Ostrich")} printText {~=%json:numberByName("Diplodocus")} end

This prints:

%json("Ostrich")=null %json("Diplodocus")=null %json:numberByName("Ostrich")=0 %json:numberByName("Diplodocus")=3

See also