17 Replies Latest reply on Jul 19, 2016 3:21 AM by Dariusz Gardynski

    Render :json in RhoMobile?

    Gerbrand Stap

      According to paragraph 2.2.8 in the Ruby on Rails Guides, rendering json can be done using 'render :json => jsondata'.

      This does not work in RhoMobile. The closest thing is 'render :string => jsondata', but this still renders html, because the result is enclosed in <div> tags. Adding ":content_type => 'application/json'" does not seem to make a difference.

      How can a controller method be changed to return json data?

       

      I would want to use this to do AJAX calls from the application.

        • Re: Render :json in RhoMobile?

          Hi,

           

          For decode a json string on Rhomobile just do this:

           

          decode_content = Rho::JSON.parse(json_content)

           

          and for encode content:

           

          hash = {"one" => 1, "two" => 2}

          json_hash = hash.to_json

            • Re: Render :json in RhoMobile?
              Gerbrand Stap

              Yes, that I know, but it is not what I want to achieve.

              I want to make a controller that might look something like this:

               

              def GetUser
                   @user = [some way to get a user]
                   render :json => @user.to_json
              end
              

               

              The GetUser method should then return Json only.

               

              When using 'render :string => @user.to_json', I get a result like '<div>{ "name" = "John Doe" }</div>'. But I don't want the div's.

                • Re: Render :json in RhoMobile?

                  I'm a newb here, but it seems like this would work:

                   

                  $myvar = @user.to_json

                   

                  render :json => :mypage

                   

                  in mypage.erg

                   

                  <%= $myvar %>

                   

                  I'd expect it to give you better control over the HTML output.

                    • Re: Render :json in RhoMobile?
                      Gerbrand Stap

                      Unfortunately this does not work. The controller still wants to render HTML in your approach.

                      The :json parameter of render seems to be completely ignored. And when the .erb file does not contain <div> tags, they are added.

                      I'm looking for a way to 'render' JSON in stead of HTML.

                        • Re: Render :json in RhoMobile?

                          Yes, it does not supported.

                           

                          You can parse json to ruby variable:

                           

                          parsed = Rho::JSON.parse("[{\"count\":10}]")

                           

                           

                           

                          And insert ‘parsed’ to erb-page

                           

                           

                           

                          http://docs.rhomobile.com/rhodes/extensions

                           

                           

                          JSON library support

                            • Re: Render :json in RhoMobile?
                              Gerbrand Stap

                              So we're stuck with rendering HTML and should strip the <div> tags?

                              Are there plans to support JSON rendering?

                               

                              My concerns are that additional tags or attributes may be added in the future, apart from the <div> tags. It just doesn't feel right to get some JSON from HTML code. So I'd rather use functionality that is supposed to yield JSON.

                                • Re: Render :json in RhoMobile?

                                  It looks like we need to fix this issue. As a spike solution you can go this way:

                                   

                                  1) put this ruby code as app/helpers/no_div_helper.rb

                                  module NoDivHelper
                                    def render(options = nil)
                                      options[:string] = options[:json] if (not options.nil?) and options[:json]
                                      content = super(options)
                                      content = content.sub(/^<div>/, '').sub(/<\/div>$/, '') if (not options.nil?) and options[:json]
                                      content   end end def self.new   super.extend NoDivHelper end

                                   

                                  2) add this to your controller class (a sample of)

                                  require 'helpers/no_div_helper'
                                  
                                  class SomeController < Rho::RhoController
                                    include NoDivHelper
                                    ...
                                    ...
                                  end
                                  

                                   

                                  3) Use it in action method

                                    def test_json
                                      render :json => '{a:1, b:2, c:3}'
                                    end
                                  
                              • Re: Render :json in RhoMobile?

                                Could you please to describe use case in more details?

                                  • Re: Render :json in RhoMobile?
                                    Gerbrand Stap

                                    I do not yet have an exact use case, but I am exploring what the features of a RhoElements v2 app are and which we could use in our application.

                                    I thought it would be useful to get data (for a search for example) from the local database or external webservice in a page. This could be done using json or by rendering the html in ruby and showing that directly. Knowing that rendering json is not supported, we probably will use the latter approach.

                                      • Re: Render :json in RhoMobile?

                                        Hi,

                                         

                                        I'm working with webservices that return and receive json data, and there's no need of render json as "render :json =>",

                                        i just work with that as i said before with:

                                        decode_content = Rho::JSON.parse(json_content)     for parse it and

                                        json_hash = hash.to_json           for encode

                                         

                                        and i didn't have any problem, also for the data stored on the database it isn't any work to add

                                        the <%= %> for show the data on the html views

                                • Re: Render :json in RhoMobile?
                                  DecisionPoint Keith Gable

                                  This response is coming at you a year later, but here's your code:

                                   

                                  def GetUser  
                                       @user = [some way to get a user]  
                                       render :json => @user.to_json  
                                  end
                                  

                                   

                                  To do this in RhoMobile:

                                   

                                  def GetUser
                                       @user = User.find(:first)
                                       @user_json = make_a_json_object # Can use ::JSON.generate({ :field1 => @user.field1 }); probably you should make a Rhom mixin that provides to_json
                                  
                                       @response["headers"]["Content-Type"] = "application/json; charset=utf-8"
                                       render :string => @user_json, :layout => false
                                  end
                                  

                                   

                                  I just wrote a tip on how to set the response content type, which you will likely want to do at some point. Check it out here: https://developer.motorolasolutions.com/thread/3313

                              • Re: Render :json in RhoMobile?
                                25c49980-cb2e-4113-a5a7-2f0078c85330

                                It looks like the following is working for me.  I found it on a forum somewhere!

                                 

                                Cheers

                                 

                                John

                                 

                                  def create

                                 

                                    @response['headers']['Content-Type']='application/json'

                                    result = ::JSON.generate( { :success => 'true', "data" =>

                                                                                       {"clientId" => "test-100",

                                                                                        "eventId" => "100" } )

                                   

                                    render :string => result, :use_layout_on_ajax => true

                                 

                                end