Parse (Json function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 15: Line 15:
<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>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>If there are errors parsing the input string, a JsonParseError exception is thrown.</li>
<li>Unicode data it 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>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 [[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.
<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.

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

%jsonJson 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"

See also