JSONItem

From Xojo Documentation
Jump to: navigation, search

Class (inherits from Object)

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
Clear IsArray
Count Load

Object Methods

These methods operate on JSONItems as objects.

Methods
Child Name Value
HasName Names
Lookup Remove

Array Methods

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

Methods
Append Remove
Insert Value

Constructors

Constructors

JSONItem.Constructor()


JSONItem.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, Booleans, or Nil.

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

Examples

JSONItem Project in the Examples folder

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

Dim 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"

Dim kids As New JSONItem
// This object is manipulated like an array
kids.Append("John Jr")
kids.Append("Jamie")
kids.Append("Jack")
kids.Append("Josie")
kids.Insert(0,"Jonah")
kids.Remove(2)
person.Value("Kids") = kids

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

TextArea1.Text = s

Create a JSONItem from a JSON String

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

Convert a Dictionary into a JSONItem

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

Dim j As JSONItem
j = d

Create a JSONItem with code and convert to a JSON String

Dim 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"

Dim kids As New JSONItem
// This object is manipulated like an array
kids.Append("John Jr")
kids.Append("Jamie")
kids.Insert(0, "Jonah")
kids.Remove(2)

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

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

See Also

Dictionary class; JSONException Runtime exception.

]http://developer.xojo.com/xojo-data$GenerateJSON Xojo.Data.GenerateJSON], Xojo.Data.ParseJSON

Personal tools
Namespaces

Variants
Actions
Main
Content
Starting Out
Dig Deeper
More Help
Toolbox
About