1

I am able to parse a JSON using the following code

$httpresult = @params['body']
$jsonresult = Rho::JSON.parse($httpresult)

But I don't know how to create a model from $jsonresult.

Jawa
  • 2,336
  • 6
  • 34
  • 39

2 Answers2

0

First, using app_info you can print the result coming from the server to check if the response is valid JSON string. Second, i think you must decode the url in order to parse it by using:

Rho::JSON.parse(Rho::RhoSupport.url_decode(@params['body']))

Wissam
  • 21
  • 2
0

Once you've the data in json_result, you can put them in a pre-existing Model. Supposing that you've already created a model with the name "Product", you can use transactions to speed up the process.

At the beginning of your module you've to require the model name:

require_source 'Product'

Then you can do this callback:

 def get_callback
    if @params['status'] == "ok"     
      json_result = Rho::JSON.parse(@params['body'])
      db = ::Rho::RHO.get_src_db('Product') 
      db.start_transaction
      Product.delete_all
      begin
        json_result.each do |item|
          Product.create({:Brand => item["B rand"], :Name => item["Name"], :SKU => d["SKU"]})
        end
        db.commit 
      rescue Exception => e
        trace_msg = e.backtrace.join("\n")
        puts 'Application initialize failed: ' + e.inspect + ";Trace: #{trace_msg}"
        db.rollback 
      end
      WebView.navigate Rho::RhoConfig.start_path
    else
      WebView.navigate url_for :action => :show_error 
    end    
  end
pfmaggi
  • 6,116
  • 22
  • 43