Parse (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:Parse subtitle}} | {{Template:Json:Parse subtitle}} | ||
This function parse serialized JSON data contained in a unicode string into a Json object tree. | |||
This | |||
==Syntax== | ==Syntax== | ||
{{Template:Json:Parse syntax}} | {{Template:Json:Parse 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 nowrap="true"><var>[%(Json):]</var></th> | <tr><th nowrap="true"><var>[%(Json):]</var></th> | ||
<td>The optional class name in parentheses denotes a [[Notation conventions for methods#Constructors|virtual constructor]] | <td>The optional class name in parentheses denotes a [[Notation conventions for methods#Constructors|virtual constructor]].</td></tr> | ||
<tr><th>unicode</th> | <tr><th>unicode</th> | ||
<td> | <td>A unicode string containing serialized JSON data.</td></tr> | ||
</table> | </table> | ||
==Usage notes== | ==Usage notes== | ||
<ul> | |||
<li>As with all virtual or true constructors, it is usually not necessary to specify the <code>%(json):</code> before the Parse method. That is <code>%json = %(json):parse(%jsonString)</code> is equivalent to <code>%json = parse(%jsonString)</code>.</li> | |||
<li>If there are errors parsing the input string, a JsonParseError exception is thrown.</li> | |||
<li>Unicode data is typically encoded either as UTF-8 or UTF-16 (the former much more common in English language apps) so would need to be converted to unicode before being parsed by the Parse method. This would be done by the [[Utf8ToUnicode (String function)|Utf8ToUnicode]] and [[Utf16ToUnicode (String function)|Utf16ToUnicode]] String functions. For example <code>%json = parse(%browserData:utf8ToUnicode)</code> coverts the browser data (presumably in a longstring) to unicode before parsing it with Parse.</li> | |||
<li>The [[JsonParse (Unicode function)|JsonParse Unicode function]] provides identical functionality to the parse method though is invoked differently (the method object is a unicode string and there ar no parameters).</li> | |||
<li>The parse method can parse JSON that consists simply of a quoted string, number, or boolean value. In these cases, the resultant Json object tree has a single object with a string, number, or boolean underlying type. If the JSON data consists simply of the unquoted string <code>null</code> Parse simply returns a null. | |||
</ul> | |||
==Examples== | ==Examples== | ||
The following example parses a JSON object and then displays the values of the items in the object: | |||
<p class="code">b | |||
%json is object json | |||
%data is unicode | |||
%data = '{"Lee": "To Kill a Mockingbird",' - | |||
'"Faulkner": ["As I Lay Dying", "Absalom, Absalom", "The Sound and the Fury"],' - | |||
'"Hurston": ["Jonah''s Gourd Vine (1934)", "Their Eyes Were Watching God"],' - | |||
'"Ellison": "Invisible Man"}' | |||
%json = parse(%data) | |||
printText {~=%json('Faulkner')(2)} | |||
printText {~=%json('Ellison')} | |||
printText {~=%json('Hurston')(1)} | |||
printText {~=%json('Lee')} | |||
end | |||
</p> | |||
This prints: | |||
<p class="code">%json('Faulkner')(2)="Absalom, Absalom" | |||
%json('Ellison')="Invisible Man" | |||
%json('Hurston')(1)="Jonah's Gourd Vine (1934)" | |||
%json('Lee')="To Kill a Mockingbird" | |||
</p> | |||
==See also== | ==See also== | ||
{{Template:Json:Parse footer}} | {{Template:Json:Parse footer}} |
Latest revision as of 00:57, 19 February 2015
Create an JSON object from serialized JSON (Json class)
[Introduced in Model 204 7.6]
This function parse serialized JSON data contained in a unicode string into a Json object tree.
Syntax
%json = [%(Json):]Parse( unicode) Throws JsonParseError
Syntax terms
%json | Json object |
---|---|
[%(Json):] | The optional class name in parentheses denotes a virtual constructor. |
unicode | A unicode string containing serialized JSON data. |
Usage notes
- As with all virtual or true constructors, it is usually not necessary to specify the
%(json):
before the Parse method. That is%json = %(json):parse(%jsonString)
is equivalent to%json = parse(%jsonString)
. - If there are errors parsing the input string, a JsonParseError exception is thrown.
- Unicode data is typically encoded either as UTF-8 or UTF-16 (the former much more common in English language apps) so would need to be converted to unicode before being parsed by the Parse method. This would be done by the Utf8ToUnicode and Utf16ToUnicode String functions. For example
%json = parse(%browserData:utf8ToUnicode)
coverts the browser data (presumably in a longstring) to unicode before parsing it with Parse. - The JsonParse Unicode function provides identical functionality to the parse method though is invoked differently (the method object is a unicode string and there ar no parameters).
- The parse method can parse JSON that consists simply of a quoted string, number, or boolean value. In these cases, the resultant Json object tree has a single object with a string, number, or boolean underlying type. If the JSON data consists simply of the unquoted string
null
Parse simply returns a null.
Examples
The following example parses a JSON object and then displays the values of the items in the object:
b %json is object json %data is unicode %data = '{"Lee": "To Kill a Mockingbird",' - '"Faulkner": ["As I Lay Dying", "Absalom, Absalom", "The Sound and the Fury"],' - '"Hurston": ["Jonahs Gourd Vine (1934)", "Their Eyes Were Watching God"],' - '"Ellison": "Invisible Man"}' %json = parse(%data) printText {~=%json('Faulkner')(2)} printText {~=%json('Ellison')} printText {~=%json('Hurston')(1)} printText {~=%json('Lee')} end
This prints:
%json('Faulkner')(2)="Absalom, Absalom" %json('Ellison')="Invisible Man" %json('Hurston')(1)="Jonah's Gourd Vine (1934)" %json('Lee')="To Kill a Mockingbird"