JSONItem

From Real Software 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
DecimalFormat IndentSpacing


Methods
Clear Load
Count ToString
IsArray


Object Methods

These methods operate on JSONItems as objects.

Methods
Child Names
HasName Remove
Lookup Value
Name

Array Methods

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

Methods
Append Value
Insert Value
Remove


Constructors

Constructors

JSONItem.Constructor()


JSONItem.Constructor(JSONString as String)



Notes

JSON is a lightweight data exchange format. It is desribed 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 and must be unique within an object.

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

The “JSON Example” project is in the Internet folder in the Example Projects folder that ships with Real Studio. It is a simple app that generates both name:value pairs (that can be manipulated like a Dictionary) and another example that is managed like a zero-based array. In the latter case, the name in the pair is the index.

The main window has a BevelButton that generates the data, a TextArea that displays it in raw form, and a ListBox that displays it as a list of elements.

The code for the BevelButton is:

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

listbox1.DeleteAllRows
ShowJSONObject(person)


The ShowJSONObject method populates the ListBox with both forms of JSONItems. The hierarchical feature of the ListBox is used to show the indexed array form.

Sub ShowJSONObject (obj as JSONItem)
for i as integer = 0 to obj.Count-1
if obj.IsArray then
listbox1.AddRow cstr(i)
listbox1.cell(listbox1.LastIndex,1) = obj.Child(i)
else
dim key as String = obj.Name(i)
if obj.value(key) isa JSONItem then
listbox1.AddFolder(key)
listbox1.RowTag(listbox1.LastIndex) = obj.Value(key)
listbox1.Expanded(listbox1.LastIndex) = true
else
listbox1.AddRow(key)
listbox1.cell(listbox1.lastindex,1) = obj.Value(key)
end if
end if
next i

The ListBox has an ExpandRow event handler that displays or hides the JSONItems as an indexed array. It simply calls ShowJSONObject by passing it the row that has the disclosure triangle.


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 new 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.

Personal tools