Completing the JSON deserializer/serializer
I was missing being able to deal with arrays and could only deal with strings in the first version of this, but have finally figured out how to deal with this. By introducing a parent object for array members I was able to treat arrays the same way as a group of key value pairs. This means that a JSON string as follows
{ "hiddendata": { "lastaccess": { "username": [ "k47", 99 ], "startedat": "2/7/2011 11:55:13 PM", "finishedat": "2/7/2011 11:46:55 PM" }, "summary": { "timeopen": "474474", "countopen": "9" } } }
translates into a cJobject that looks like this
hiddendata
hiddendata.lastaccess
hiddendata.lastaccess.username
hiddendata.lastaccess.username._array
hiddendata.lastaccess.username._array.1 fhk647
hiddendata.lastaccess.username._array.2 99
hiddendata.lastaccess.startedat 2/7/2011 11:55:13 PM
hiddendata.lastaccess.finishedat 2/7/2011 11:46:55 PM
hiddendata.summary
hiddendata.summary.timeopen 474474
hiddendata.summary.countopen 9
and can be accessed in various ways such as the below
set cj = new cObject cj.deserialize(JSONstring) With cj.child("lastaccess") .child("username._array").children(1).asString .child("username").child("_array").child("2").asLong .child("startedat").asDate .child("finishedat").Value end With With cj.child("summary") .child("timeopen").value .child("countopen").value end With
I haven’t bothered with date decoding for now. Next step is to test all this with a whole bunch of JSON examples