JSON parsing is hard..

This week I ran into my first roadblock on this project. I was tasked with linking the barcode scanning to the Barcode Lookup API. This proved to be a challenge in 2 ways. On the scanning-side, I needed to be able to translate a barcode scan into a usable data type like a String. On the API side, I needed to be able to effectively make the API calls and parse the response data.

First, I took my partner’s advice to use my computer’s webcam as the camera for my Android emulator to test out the barcode scanning library. This advice worked out well and I was able to link up my webcam to the emulator through the Android Studios AVD manager. Then, I attempted to scan various barcodes I had near me. I attempted scanning a tin of Altoids, Jergen lotion, and a book. None of these resulted in a scan. The scanning simply does not return any result. Next week I plan on trouble shooting this.

On the API side, I struggled initially with parsing the JSON response. The JSON structure is a bit tricky since it is a large single JSON object with a “products” data. In “products” there is an array of objects, each being individual products that are relevant to the search keyword. This JSON structure required me to create a SearchResult class which contain “products” then a separate Product class which housed the data for each product in the array. I was kept receiving an error due to an array of image urls for each product. I eventually was able to deduce that this error was due to the type I was casting for this array. I had to specifically cast it as a List<String>() in order for the Product class to not return an error.

I was able to successfully print out product titles after the error was fixed. Next up, searching of products!

Leave a comment

Your email address will not be published. Required fields are marked *