A class to parse text formatted in JSON format (Javascript Object Notation) into an internal object tree or array representation, manipulate this data, and than convert it back into JSON string format. JavaScript Object Notation is a minimal, readable format for structuring data. It is used primarily to transmit data between a server and web application.

Syntax

[<oRef>] = new JSON(<cString>)

<oRef>

A variable or property in which to store a reference to the newly created JSON object.

<cString>

A reference to the string that the JSON object can be used to automatically parse the initial members or array objects.

 

Properties

Property

Default

Description

baseClassName

JSON

Identifies the object as an instance of the JSON class

className

(JSON)

Identifies the object as an instance of a custom class. When no custom class exists, defaults to baseClassName

member

null

a reference to the member object used to access the current member within a JSON object if a JSON object was contained in the parsed JSON text.

Method

Parameters

Description

getErrorOffset()

 

returns numeric offset into JSON string where error was encountered

getParseError()

 

returns string describing error (only available in English)

getParseErrorCode()

 

returns numeric error code

hasParseError()

 

returns True if last call to Parse() method encountered an error

parse()

<cMembers>

parses JSON string into internal JSON object or JSON array (depending on what is defined in the JSON string)
if NOT using <Obj> = new JSON(cMembers) ... then use ...
<Obj> = new JSON()
<Obj>.parse(cMembers)

isArray()

 

returns True if the last parsed JSON text contained  a JSON array

isEmpty()

 

returns True if the dBASE JSON() instance does not contain any JSON data

isObject()

 

returns True if the last parsed JSON text contained a JSON member object

stringify()

 

generates a JSON string from the current state of the contents of a JSON() object instance

 

 

Methods to use when isObject() returns True

 

Method

Parameters

Description

addMember()

<key string>, <value>

adds new member <key string> with value <value> to object

findMember()

<Key string>

searches for <key string>.

returns True if found,

otherwise, returns False.

If found, sets member property to reference the found member

firstKey()

 

returns key string for first member of JSON object

firstMember()

 

sets member property to first member of object

hasMember()

<key string>

returns True if <key string> is found within the top level members of the object tree, otherwise, returns False.

lastMember()

 

sets member property to last member of object

memberCount()

 

returns the number of top level members within a JSON object, otherwise returns zero

nextKey()

<key string>

returns key string for next key string after <key string>. This does not set the member property. To do that you need to use findMember. For example...

... if J is JSON Object...

cKey = j.firstKey()

cKey2 = j.nextKey(cKey)

j.findMember(cKey2)

nextMember()

 

sets member property to next member of object after the member currently referenced

previousMember()

 

sets member property to previous member of object before the member currently referenced

removeAllMembers()

 

removes all members from JSON object

removeMember()

<key string>

removes member <key string> from JSON object

setMemberValue()

<Key string>,<value>

changes <value> assigned to member <key string>

 

 

Methods to use when isArray() returns True

 

Method

Parameters

Description

append()

 <value>

adds new element to JSON Array and assigns <value> to it

capacity()

 

returns number of elements that can fit in the memory currently allocated for a JSON Array

clear()

 

 deletes all elements of JSON Array

delete()

nIndex

deletes array element <nindex> from JSON Array

getAt()

<nIndex>

returns value of array element specified by <nindex> (<nindex> is 1 based).

reserve()

<capacity>

allocates enough memory to hold <capacity> elements in a JSON Array

setAt()

<index>,<value>

returns True if able to assign <value> to array element <index>, otherwise, returns False.

size()

 

returns number of elements currently in the array

 

Description

 

There are two types of data that JSON can use. MEMBERS and ARRAYS.

Here are ways to create json objects...

 

MEMBERS

 

cMembers = '{"ID" : "1010", "Name" : "Marty", "Address" : "123 Main Street" }'

j = new JSON(cMembers)

or

cMembers = '{"ID" : "1010", "Name" : "Marty", "Address" : "123 Main Street" }'

j = new JSON()

j.parse(cMembers)

 

IMPORTANT NOTE: the string of members must be in this format ...

 

members all surrounded by single quotes surrounding curly braces with the members listed like so ..

'{ "MEMBER_ID" : "MEMBER VALUE" [, next member , ...] }'

each member must have it's ID and value delimited with double quotes.

Each ID and Value separated by a colon.

Each Member separated by a comma.

 

ARRAYS

 

a = '[1,2,3,4,5]'

//or

a = '["one","two","three",4]'

jsA = new json(a)

OR

a = '["one","two","three",4]'

jsA.parse(a)

 

IMPORTANT NOTE: for an array you must use single quotes around square brackets around each array item separated by a comma.

 

SUBOBJECTS

you can have subobjects in a member json object.

You have to reference a subobject as a new member with this syntax ...


"<subObj>" : { "<member1>" : 123, "<member2>" : "value"}

values can be either numbers or characters. You can have many members.

Example....

//to findmember with sub object

var = '{"ID" : "1010", "Name" : "Marty", "Address" : "123 Main Street",  "subObj" : { "prop1" : 123, "prop2" : 555} }'

js = new json()

js.parse(var)

?"find subObj",js.findmember("subObj")

?js.member.name,js.member.value

?"isObject",js.member.isobject()

?"find prop1",js.member.findmember("prop1")

?"name and value",js.member.member.name,js.member.member.value

?"find prop2",js.member.findmember("prop2")

?"name and value",js.member.member.name,js.member.member.value

 

In order to add a subobject to an existing JSON object, you must create a second json() object containing the content of the subobject.

 

Example...

 

s = '{"Sub1":"abc", "Sub2": "def"}'

o = new json(s)

sparent = '{"Memb1":"Val1", "Memb2":"Val2"}'

oparent = new json( sparent )

oparent.addmember("Memb3", o)