JSON Help Needed

Hey guys,

I’ve never done anything with parsing JSON strings before but I’m starting to integrate my software with some services provided by a vendor company I have partnered with. They have an API to access a database that returns a JSON string. Below at the bottom is an example of what I get back.

I’ve tried putting this string into a JSON object using:

Dim r as New JSONItem(data)

Dim DataNames() as String = r.Names

Or

Dim r as New JSONItem
r.load(data)

Dim DataNames() as String = r.Names

Where data is the string. It does not seem to work quite correctly. DataNames never gets any data.

So what am I doing wrong and how can I easily parse the monster I have below?

Thanks,

Jon

{"$id":"1","Brand":"Sony","Type":"AV System","Links":[{"$id":"2","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/AV%20System"},{"$id":"3","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/AV%20System/models"}]},{"$id":"4","Brand":"Sony","Type":"Aux","Links":[{"$id":"5","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Aux"},{"$id":"6","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Aux/models"}]},{"$id":"7","Brand":"Sony","Type":"Blu Ray","Links":[{"$id":"8","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Blu%20Ray"},{"$id":"9","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Blu%20Ray/models"}]},{"$id":"10","Brand":"Sony","Type":"CD","Links":[{"$id":"11","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/CD"},{"$id":"12","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/CD/models"}]},{"$id":"13","Brand":"Sony","Type":"DVD","Links":[{"$id":"14","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/DVD"},{"$id":"15","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/DVD/models"}]},{"$id":"16","Brand":"Sony","Type":"DVD/VCR Combo","Links":[{"$id":"17","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/DVDxfslxVCR%20Combo"},{"$id":"18","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/DVDxfslxVCR%20Combo/models"}]},{"$id":"19","Brand":"Sony","Type":"Game","Links":[{"$id":"20","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Game"},{"$id":"21","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Game/models"}]},{"$id":"22","Brand":"Sony","Type":"Home Theater System","Links":[{"$id":"23","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Home%20Theater%20System"},{"$id":"24","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Home%20Theater%20System/models"}]},{"$id":"25","Brand":"Sony","Type":"IPod","Links":[{"$id":"26","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/IPod"},{"$id":"27","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/IPod/models"}]},{"$id":"28","Brand":"Sony","Type":"Laser Disc","Links":[{"$id":"29","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Laser%20Disc"},{"$id":"30","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Laser%20Disc/models"}]},{"$id":"31","Brand":"Sony","Type":"Media Manager","Links":[{"$id":"32","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Media%20Manager"},{"$id":"33","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Media%20Manager/models"}]},{"$id":"34","Brand":"Sony","Type":"Multizone System","Links":[{"$id":"35","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Multizone%20System"},{"$id":"36","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Multizone%20System/models"}]},{"$id":"37","Brand":"Sony","Type":"Projector","Links":[{"$id":"38","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Projector"},{"$id":"39","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Projector/models"}]},{"$id":"40","Brand":"Sony","Type":"Receiver/Preamp","Links":[{"$id":"41","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/ReceiverxfslxPreamp"},{"$id":"42","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/ReceiverxfslxPreamp/models"}]},{"$id":"43","Brand":"Sony","Type":"Set Top Box","Links":[{"$id":"44","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Set%20Top%20Box"},{"$id":"45","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Set%20Top%20Box/models"}]},{"$id":"46","Brand":"Sony","Type":"Soundbar","Links":[{"$id":"47","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Soundbar"},{"$id":"48","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Soundbar/models"}]},{"$id":"49","Brand":"Sony","Type":"TV","Links":[{"$id":"50","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/TV"},{"$id":"51","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/TV/models"}]},{"$id":"52","Brand":"Sony","Type":"TV/DVD Combo","Links":[{"$id":"53","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/TVxfslxDVD%20Combo"},{"$id":"54","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/TVxfslxDVD%20Combo/models"}]},{"$id":"55","Brand":"Sony","Type":"Tuner","Links":[{"$id":"56","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Tuner"},{"$id":"57","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/Tuner/models"}]},{"$id":"58","Brand":"Sony","Type":"VCR","Links":[{"$id":"59","Rel":"self","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/VCR"},{"$id":"60","Rel":"models","Href":"https://irdb.globalcache.com:8081/api/brands/Sony/types/VCR/models"}]}

This isn’t one JSON object, it’s many. To turn it into one, add “[” at the beginning, then “]” at the end.

That makes it an array, each element of which is an Object (think “Dictionary”), so you would have to dive into each Child to get the names…

The equivalent Xojo structure (after you add the brackets) is an Array of Variant, each element of which is a Dictionary.

BTW, be sure to check out my open-source project, JSONItem_MTC. It’s exactly like JSONItem but fixes a couple of bugs and is faster.

https://github.com/ktekinay/JSONItem_MTC

Thanks, Kem. I appreciate it. I think this will get me going in the right direction. I understand completely what you mean by each array item holding an object - which would be the individual JSON item. So then I just need to iterate the array and pull the names from each element in the array. Makes total sense.

I’ll check out your project too. Thanks!

[quote=131701:@Kem Tekinay]This isn’t one JSON object, it’s many. To turn it into one, add “[” at the beginning, then “]” at the end.

That makes it an array, each element of which is an Object (think “Dictionary”), so you would have to dive into each Child to get the names…

The equivalent Xojo structure (after you add the brackets) is an Array of Variant, each element of which is a Dictionary.

BTW, be sure to check out my open-source project, JSONItem_MTC. It’s exactly like JSONItem but fixes a couple of bugs and is faster.

https://github.com/ktekinay/JSONItem_MTC[/quote]

Thanks again, Kem.

I’ve now got all my code working any have got the API from my vendor completely integrated into my application. Once you pointed me in the right direction and I got my feet wet with a few tests from a test app, I was ready to go. Parsing JSON strings isn’t really that difficult once you understand how they can be embedded within each other and all. And since the API I am using is a fixed and well defined API, I don’t have to attempt to search for the key values I need as I already know them.

Good learning experience!

And I have your class dropped into my project as well. It does seem slightly faster. But the longest part of the deal is simply getting the data back from my vendor’s server.

Oh the little things that can make life joyous! :smiley:

Jon,

did you check the example folder ? There is an example that (build and) read a JSON string and populate a ListBox with its contents. It even use AddRow AND AddFolder when needed (the Kids entry).

Marvelous example.

[quote=131838:@Emile Schwarz]Jon,

did you check the example folder ? There is an example that (build and) read a JSON string and populate a ListBox with its contents. It even use AddRow AND AddFolder when needed (the Kids entry).

Marvelous example.[/quote]

Thanks, Emile. Yes, I have. Unfortunately, it’s not set up to handle my string above as my string contains embedded JSON Items as Kem pointed out. But thanks to Kem’s help, I have been able to successfully integrate everything into my app and it’s working really well.

Other than the fact that I have to initially wait for the initial downloads, things are working quickly. I have to use the HTTPSocket to log me into the server. Then I have to initially download the first JSON string which is an array of almost 1000 additional JSON objects and then process all those into rows on a pop-up menu. It takes a couple seconds for that to take place. After that - all is good.