Building on an open-source library from Charcoal Design, we created a new class named JSONItem. For those of you that don't already know, JSON objects can either contain named items (like a dictionary) or indexed items (like an array). To make these objects a little easier to create, we've added methods so you can manipulate them either way. Values can be Strings, Numbers, Booleans, Nil or another JSONItem.
For instance:
//Like a Dictionary
Dim Person as new JSONItem
Person.Value("Name") = "John Doe"
Person.Value("Married") = True
Person.Value("Age") = 35
Person.Value("Spouse") = "Jane Doe"
//Like an array
Dim Kids as new JSONItem
Kids.Append "James"
Kids.Append "Julie"
Kids.Append "Joe"
Kids.Append "Jessica"
//Add the Kids JSONItem to the parent JSONItem
Person.Value("Kids") = Kids
//Create a JSON string from the defined object
Dim JSONString as String = Person.ToString()
JSONString = {"Name":"John Doe","Married":true,"Age":35,"Spouse":"Jane Doe","Kids":["James","Julie","Joe","Jessica"]}
In addition to creating JSON Strings, JSONItem can parse them. Once loaded, you can walk through the object as if you were traversing a folderitem tree. Using the example above:
Person = new JSONItem(JSONString) //From the example above
Dim MyKids as JSONItem = Person.Child("Kids")
For i As Integer = 0 to MyKids.Count-1
MsgBox MyKids.Value(i)
Next i
Services that you can communicate with using JSON include Google, WordPress, Twitter, YouTube and Flickr.
9 comments:
Huzzah! I had to hack together a JSON parser a few months ago from some ancient RB code I found. I'll be glad to be able to scrap that monstrosity.
I assume there's also a function to get the key's name for a retrieved JSONItem?
Excellent. I've been using Charcoal Design's JSON object for a while now. Nice to see it being officially supported now. Wonder if it supports chaining correctly as the dictionary object does not. One has to "extends" it to return a variant in order to get it to return additional dictionary objects. I.e. a series of dictionaries to access an integer like this:
t = dict("windows").value("positions").value("top")
does not work unless you 'correct' the dictionary object by overriding the value method with a variant returning one of your own.
To answer your questions
1. You can get a list of keys using the Names() method
2. To use the chaining, you would do something like this:
t = js.item("windows").item("positions").value("top")
The following is my json file:
{"chapter": {"title":"Blaaaaaa","content":"8B57CF2049C5D09C8963B513F48913457D2D026CFD371F3442EC279DE143EB8F75EEC405C9713BB89C7CCDCCDBB9D7F87FB4D3F16F7AFE3DEDBA6893CEDCF75A2B9AA4EFFA98A47E2A0DA7C107B61AB7F61A0B0C9ABA22A3169E1CEC5AA45AE0B5ADFDFD75BEC45F99092B22E430DC4A73E5416FED929641686C45B4E43FAB4D2024643C75A69475F326C3A39FC0EE04BA296549D05C74D5885468CD990F66DF7CA96C3778F01955D8127EDBD4A48538D8F196EEB9F83DCB8742CB158BD1C9A7413831B0A9657667F9E9A16E5A80FC4D28DFB47FB9D10506A67BF1B3ED44673E6C8C633C7839FA1052FD7FDEC76C8C087385BD6B682276F37217B32439EDA5F0308F7664EB19390A"}}
How would I parse this so I would get Title and content? The format is valid, but is it valid for rb? I cant get it to work :( And JSON documentation is still missing!!
sorry got cut off:
{"chapter": {"title":"Blaaaaaa","content":"8Bkcmx"}}
To those that have the same issue:
title.text = chapter.Child("chapter").Value("title")
content = chapter.Child("chapter").Value("content")
DOCUMENTATION WOULD BE NICE WOULDNT IT??But the above will work to get the separate values of your keys.
Just read this article. The JSON lib written by Douglas Crockford has a bug in string handling. I've fixed it about a year ago and told him, but didn't receive his reply. Was that found and fixed in Real Studio?
Post a Comment