Help with JSONItems

I have a JSON string looking like this:

{
“appDesc”: {
“description”: “App description.”,
“message”: “Say hi”
},
“appName”: {
“description”: “App Name.”,
“message”: “HelloWorld”
}
}

I’ve read this from a file, and have read the key “appName” from a different file. I need to find the “appName” item in this JSON data, then find the “message” item inside it. Here’s what I’ve tried:

      masterJSON = new JSONItem(jsonData)
      appNameJSON = masterJSON(nameKey, nil)
      appName = DefineEncoding(appNameJSON.Lookup("message", ""), Encodings.UTF8)

(nameKey is a string variable obtained elsewhere, appName is also a string. The masterJSON and appNameJSON items are JSONItem variables.)

Everything seems to be working, except for one problem… in some cases, the nameKey has a different case than what’s in the JSON data. For example, I may be given a key “APPNAME”, and have to match that to the “appName” key in the JSON data. In this case, my code fails.

I have also tried iterating over all items in masterJSON, using masterJSON.Name. I convert both nameKey and the string returned from masterJSON.Name to lowercase, and if they match, I call masterJSON.Child with the same index… and get on OutOfBoundsException on that call. No idea why.

Any tips? How do I do this?

I think this is the right idea, but you don’t have to convert to lowercase, and you can use Value instead of Child.

Off the top of my head:

dim names() as string = masterJSON.Names
dim child as JSONItem
dim pos as integer = names.IndexOf( appName )
if pos <> -1 then
  child = masterJSON.Value( names( pos ) )
end if

if child <> nil then …

BTW, while this will work, if Names contains both “appName” and “APPNAME”, you will get whichever comes up first in the array.

Well, that worked perfectly, thanks!

I’ve gotta say, though, this JSONItem class needs a bit of work… it’s very unintuitive and quirky!

That’s not a concern in this case… there will only be one item with that name in this JSON data.

[quote=124610:@Thomas Reed]Well, that worked perfectly, thanks!

I’ve gotta say, though, this JSONItem class needs a bit of work… it’s very unintuitive and quirky![/quote]

I’ve been doing a lot of work with the JSONItem class recently and I’ve come to think of it as very intuitive since you can use it as either an array or case-sensitive Dictionary, both of which are familiar concepts.

What are you finding unintuitive?