Last Updated: February 25, 2016
·
728
· stevebenner

Load and parse JSON file in Rhodes

Perhaps you are developing a mobile app using Rhodes, and you find yourself wishing to manipulate data via local JSON, perhaps for a configuration file or loggging purposes. Since the Rhodes API is not so well documented (especially the JSON methods) and modules must occaisionally be called directly, I've been using syntax sugar to abstract away much of the headache:

require 'json'

# You can use the Application API to access local directory paths reliably
app_dir = Rho::Application.appBundleFolder
public = Rho::Application.publicFolder

def parse_json(filename)
  # If you're a rubyist, then File.join is most likely familiar to you
  Rho::JSON.parse Rho::RhoFile.read Rho::RhoFile.join(app_dir, filename)
end

_# res = parse_json('config.json') => {'some_key' => 'some_value'}_