Type (Json function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
(Automatically generated page update)
 
No edit summary
Line 1: Line 1:
{{Template:Json:Type subtitle}}
{{Template:Json:Type subtitle}}
 
This function returns the [[Json class#The_JsonType_enumeration|type]] of a Json object.
This page is [[under construction]].
==Syntax==
==Syntax==
{{Template:Json:Type syntax}}
{{Template:Json:Type syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%jsonType</th><td><var>JsonType</var> value</td></tr>
<tr><th>%jsonType</th><td><var>JsonType</var> enumeration value of the Json object type. If the Json object is null, the returned value is null.</td></tr>
<tr><th>Json</th>
<tr><th>Json</th>
<td>Json object, which may be <var>Null</var></td></tr>
<td>Json object, which may be <var>Null</var> or any underlying Json object type (boolean, number, string, array, or object).</td></tr>
</table>
</table>
==Usage notes==
==Usage notes==
<ul>
<li>One would assume the type of a Json object would normally be established as part of the interface between the programs exchanging JSON data. As such, Type might not to be used very frequently. If the source of the JSON data is considered potentially unreliable then, rather than testing Type for each object, one might instead wrap all object references inside a Try/Catch for an [[InvalidJsonType class|InvalidJsonType exception]].</li>
</ul>
==Examples==
==Examples==
The following example steps through an object and either prints the object string value of the object item (property) or the first item in the item (property) array, if it's an array. The determination of which to do is based on the result of the Type method:
<p class="code">b
%json        is object json
%title      is string len 64
%i          is float
%json = object
%json("Lee") = "To Kill a Mockingbird"
%json("Faulkner") = array("As I Lay Dying", "Absalom, Absalom", "The Sound and the Fury")
%json("Hurston") = array("Jonah's Gourd Vine", "Their Eyes Were Watching God")
%json("Ellison") = "Invisible Man"
for %i from 1 to %json:count
  if %json:itemByNumber(%i):type eq array then
      %title = %json:itemByNumber(%i)(1):stringValue
  else
      %title = %json:itemByNumber(%i):stringValue
  end if
  printText {%json:nameByNumber(%i)}: {%title}
end for
end
</p>
This prints
<p class="code">Lee: To Kill a Mockingbird
Faulkner: As I Lay Dying
Hurston: Jonah's Gourd Vine
Ellison: Invisible Man
</p>
==See also==
==See also==
{{Template:Json:Type footer}}
{{Template:Json:Type footer}}

Revision as of 23:19, 18 February 2015

Get type of JSON object (Json class)

[Introduced in Model 204 7.6]

This function returns the type of a Json object.

Syntax

%jsonType = json:Type

Syntax terms

%jsonTypeJsonType enumeration value of the Json object type. If the Json object is null, the returned value is null.
Json Json object, which may be Null or any underlying Json object type (boolean, number, string, array, or object).

Usage notes

  • One would assume the type of a Json object would normally be established as part of the interface between the programs exchanging JSON data. As such, Type might not to be used very frequently. If the source of the JSON data is considered potentially unreliable then, rather than testing Type for each object, one might instead wrap all object references inside a Try/Catch for an InvalidJsonType exception.

Examples

The following example steps through an object and either prints the object string value of the object item (property) or the first item in the item (property) array, if it's an array. The determination of which to do is based on the result of the Type method:

b %json is object json %title is string len 64 %i is float %json = object %json("Lee") = "To Kill a Mockingbird" %json("Faulkner") = array("As I Lay Dying", "Absalom, Absalom", "The Sound and the Fury") %json("Hurston") = array("Jonah's Gourd Vine", "Their Eyes Were Watching God") %json("Ellison") = "Invisible Man" for %i from 1 to %json:count if %json:itemByNumber(%i):type eq array then %title = %json:itemByNumber(%i)(1):stringValue else %title = %json:itemByNumber(%i):stringValue end if printText {%json:nameByNumber(%i)}: {%title} end for end

This prints

Lee: To Kill a Mockingbird Faulkner: As I Lay Dying Hurston: Jonah's Gourd Vine Ellison: Invisible Man

See also