Useful JSON API methods

The following table lists and describes some useful JSON API methods and properties.

Class

Method or property

Arguments

Description

java.net.URL

JsonContent

Not applicable

The getter for this property:

  • Makes the HTTP or other URL request from the internet and gets the result as text.
  • Parses the JSON text into a javax.script.Bindings object.
  • Coerces this Bindings object to the type dynamic.Dynamic before returning the object. For this dynamic type, Gosu uses type inference when assigning the result to a variable.

For example:

var person: Dynamic = personUrl.JsonContent

gw.lang.reflect.json.Json

fromJson

String

Parses JSON data as a String and converts it to the structural type. To use natural Gosu syntax for properties of the result, assign the result to a variable of type dynamic.Dynamic. For example:

var j : Dynamic = Json.fromJson(jsonText)

fromJsonFile

java.io.File

Get JSON data from a File object and convert it to the structural type. To use natural Gosu syntax for properties of the result, assign the result to a variable of type dynamic.Dynamic.

fromJsonUrl

String or java.net.URL

Gets JSON data from a URL and convert it to the structural type. One method signature takes a URL as a String, and one method signature takes the URL as a java.net.URL object. To use natural Gosu syntax for properties of the result, assign the result to a variable of type dynamic.Dynamic.

dynamic.Dynamic

toGosu

No arguments

Converts an instance of the structural type to Gosu code that creates that instance. The return value is a String object containing Gosu code.

Escaped Unicode characters in the structural type remain unchanged in the Gosu code.

toJson

No arguments

Converts an instance of the structural type to JSON. The return value is a String object containing JSON data.

Escaped Unicode characters in the structural type remain unchanged in the JSON data.

toXml

No arguments

Converts an instance of the structural type to XML. The return value is a String object containing XML data.

Escaped Unicode characters in the structural type are converted to unescaped Unicode characters in the XML data.