JSONItem

From Xojo Documentation

Class (inherits from Object)


New in 2011r2

Used for parsing and creating Javascript Object Notation (JSON) strings. A JSONItem can use either named keys or indexed values (like an array), but not both. See the Notes section for more information.

Properties

Properties
Compact EscapeSlashes ToString
DecimalFormat IndentSpacing
Methods
Count Load
IsArray RemoveAll

Object Methods

These methods operate on JSONItems as objects.

Methods
Child Lookup Remove
ChildAt NameAt Value
HasName Names

Array Methods

These methods operate on JSONItems as arrays. A JSONItem array is zero-based.

Methods
Add RemoveAt
AddAt ValueAt

Constructors

Constructors

Constructor()


Constructor(JSONString as String)


Notes

JSON is a lightweight data exchange format. It is described at http://json.org. It is based on the Javascript language and uses two structures.

JSON Objects can contain named data a collection of name-value pairs (like a Dictionary) as well as indexed data (like an array). To facilitate this, JSONItems can manipulate data in either way with the following restrictions:

  • The first element that you add to a JSONItem determines its type.
  • Array objects can be accessed only by index.
  • A JSONItem’s type cannot be changed without resetting the object with the Clear method.

Names must be Strings, must be unique within an object and are case-sensitive.

Values can be any of the following types: Strings, numbers, JSONItems, arrays (string, number or boolean), Booleans, or Nil.

This class is based on code by Charcoal Design (http://www.charcoaldesign.co.uk/source/realbasic)

Sample Code

JSONItem Project in the Examples folder

This code populates a JSONItem with data and then displays it in its raw form in a TextArea:

Var person As New JSONItem
// This object is manipulated like a dictionary
person.Value("Name") = "John Doe"
person.Value("Age") = 32
person.Value("Married") = True
person.Value("Spouse") = "Jane Doe"

Var kids As New JSONItem
// This object is manipulated like an array
kids.Add("John Jr")
kids.Add("Jamie")
kids.Add("Jack")
kids.Add("Josie")
kids.AddAt(0,"Jonah")
kids.RemoveAt(2)
person.Value("Kids") = kids

Person.Compact = True
Var s As String = person.ToString

TextArea1.Text = s

Create a JSONItem from a JSON String

Var js As String = "{""Name"":""John Doe"",""Age"":32,""Kids"":[""Jonah"",""John Jr""],""Married"":true,""Spouse"":""Jane Doe""}"
Var j As New JSONItem(js)

Convert a Dictionary into a JSONItem

Var d As New Dictionary
d.Value("Name") = "John Doe"
d.Value("Age") = 32
d.Value("Married") = True
d.Value("Spouse") = "Jane Doe"

Var j As JSONItem
j = d

Create a JSONItem with code and convert to a JSON String

Var person As New JSONItem
// This object is manipulated like a dictionary
person.Value("Name") = "John Doe"
person.Value("Age") = 32
person.Value("Married") = True
person.Value("Spouse") = "Jane Doe"

Var kids As New JSONItem
// This object is manipulated like an array
kids.Add("John Jr")
kids.Add("Jamie")
kids.AddAt(0, "Jonah")
kids.RemoveAt(2)

// Add the Kids object to the Person object
person.Value("Kids") = kids

// Convert to a JSON String
Var s As String = person.ToString

See Also

Dictionary class; Xojo.Data.GenerateJSON, Xojo.Data.ParseJSON methods; JSONException Runtime exception.