An example using the JSON object with members..

 

//you must start with a single quote around the json list of keys and members..

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

//NOTE: KEY values 'like ID and Name' are CASE SENSITIVE

//Instantiate the json object

js = new json()

//or js = new json(var)

//parse populates the object with the variable string above

js.parse(var)

nmembers = js.membercount()

?js.firstmember()

for x = 1 to nmembers

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

js.nextMember()

endfor

if NOT js.hasmember("PHONE")

js.addMember("PHONE","000-000-0000")

?js.findMember("PHONE")

?js.member.value

endif

js.setMemberValue("PHONE" , "123-456-7892")

?js.findmember("PHONE")

?js.member.value

?

//remember the key is case sensitive..

js.setMemberValue("Name","KATHY")

?js.findMember("Name")

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

?

 

An example using JSON object with Array items..

 

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

jsA = new json()

jsA.parse(a)

?"jsA is array ",jsA.isArray()

?jsA.getat(1)

?"adding json array"

js.addMember("ARRAY",jsA)

?js.findMember("ARRAY")

?js.member.getat(1)

?

 

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

 

Adding an array as member to an already existing JSON object...

 

a = '[]'

ja = new json(a)

?ja.stringify()

//as an object...

j = new json()

j.addmember("cArray",ja)

?j.findmember("cArray")

?j.member.isArray()

?

?j.stringify()

 

 

An example with function for iterating through the json object and output all data...

 

cMembers = '{;

   "glossary": {;

       "title": "example glossary",;

       "GlossDiv": {;

            "title": "S",;

            "GlossList": {;

                "GlossEntry": {;

                    "ID": "SGML",;

                    "SortAs": "SGML",;

                    "GlossTerm": "Standard Generalized Markup Language",;

                    "Acronym": "SGML",;

                    "Abbrev": "ISO 8879:1986",;

                    "GlossDef": {;

                        "para": "A meta-markup language, used to create markup languages such as DocBook.",;

                        "GlossSeeAlso": ["GML", "XML",{"obj1": "object1"}];

                    },;

                    "GlossSee": "markup";

                };

            };

        };

    };

}'

j = new json()

j.parse(cMembers)

 

//Check for Errors in string

if j.hasParseError() //- returns True if last call to Parse() method encountered an error

   ?"error code: "+j.getParseErrorCode()//- returns numeric error code

   ?"error offset: "+j.getErrorOffset() //- returns numeric offset into JSON string where error was encountered

   ?"parse error: "+j.getParseError() //- returns string describing error (only available in English)

else

   jsonListContent(j)

endif

 

// Iterate through json object and its subobjects and json arrays

// to display all of its content.

// Each call iterates through one level of the json document tree

//    If a sub object or array is found, function calls itself recursively

function jsonListContent

   parameters j

   private x, xtype

   static depth = -1, indent = 3

   jtype = type("j")

   depth++

   

   if jtype == 'O'

      // dBASE Object     

      if j.classname $ 'JSON|JSONMEMBER'

         if j.isObject()

            if j.membercount() > 0

               j.firstmember()

               for x=1 to j.membercount()                  

                  if j.member.isObject() or j.member.isArray()

                     // Recursively call this function for subobject or array

                     // Value will print as 'Object' or 'Array'

                     ?

                     ?space(depth*indent)+"*** "+j.member.name+" ***"

                     jsonListContent(j.member)

                  else

                     ? space(depth*indent)+j.member.name, j.member.value

                  endif

                  j.nextmember()

               endfor

            endif

            

         elseif j.isArray()

            if j.size() > 0

               for x = 1 to j.size()

                  etype = j.getat(x)

                  if etype == 'Object'

                     jsonListContent(j.getat(x))

                  else

                     ?space(depth*indent)+j.getat(x)

                  endif

               endfor

            endif

         endif

      endif

   endif

?

   depth--

 

   return