JsonParse (Unicode function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
No edit summary
 
Line 43: Line 43:
</p>
</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