JsonParse (Unicode function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
(Automatically generated page update)
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
{{Template:Unicode:JsonParse subtitle}}
{{Template:Unicode:JsonParse subtitle}}
 
This function parse serialized JSON data contained in a unicode string into a [[Json class|Json]] object tree.
This page is [[under construction]].
==Syntax==
==Syntax==
{{Template:Unicode:JsonParse syntax}}
{{Template:Unicode:JsonParse 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>unicode</th>
<tr><th>unicode</th>
<td>Unicode</td></tr>
<td>A unicode string containing serialized JSON data.</td></tr>
</table>
</table>
==Usage notes==
==Usage notes==
<ul>
<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 [[Parse (Json function)|Parse Json function]] provides identical functionality to the JsonParse method though is invoked differently (it is a virtual constructor with the JSON unicode string as the first parameter).</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 = %data:jsonParse
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==
<table class="list"><tr>
<td valign="top"><ul style="margin-top:0px;">
<li>[[Json class]]
</ul></td>
<td valign="top"><ul style="margin-top:0px;">
<li>[[List of Json methods]]
</ul></td>
<td valign="top"><ul style="margin-top:0px;">
<li>[[Json methods syntax]]
</ul></td>
</tr><tr>
</tr>
</table>
{{Template:Unicode:JsonParse footer}}
{{Template:Unicode:JsonParse footer}}

Latest revision as of 01:01, 19 February 2015

Parse a string into a Json object tree (Unicode 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 = unicode:JsonParse Throws JsonParseError

Syntax terms

%jsonJson object
unicode A unicode string containing serialized JSON data.

Usage notes

  • 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 Parse Json function provides identical functionality to the JsonParse method though is invoked differently (it is a virtual constructor with the JSON unicode string as the first parameter).
  • 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 = %data:jsonParse 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"

See also