JSONItem
From Xojo Documentation
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 | ||
|
Methods | |||||||||||||||||
|
Constructors | |||
|
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.
Per the JSON spec, strings must be UTF8-encoded. Unless you are specifically encoding them differently, they will be UTF8 by default.
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:
// 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 j As New JSONItem(js)
Convert a Dictionary into a JSONItem
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
// 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; GenerateJSON, ParseJSON methods; JSONException.