3.5.12 code / concepts to read JSON data no longer working in 4.0


Audience

Scenario:  In 3.5.12, with the proper variables filled in, this code:

    Rho::AsyncHttp.get(
      :url => @appconfig.mainserverurl + "AppConfig/GetServerSettings?useremail=" + @appconfig.useremail + "&password=" + @appconfig.password,
      :callback => url_for(:controller => :AppConfig, :action => :serversettings_callback)
    )

would allow me to examine the JSON returned in the body portion of @params, like :

  def serversettings_callback
    if @params["status"] == "ok"
    
      returnparams = @params['body']
   
      if returnparams["authorized"] != "true"

      ... other code below

So now that I am attempting to use 4.0, I modified my get code to this:

    setshash = Hash.new
    setshash['url'] = @appconfig.mainserverurl + "AppConfig/GetServerSettings?useremail=" + @appconfig.useremail + "&password=" + @appconfig.password
    setshash['headers'] = {"Content-Type" => "application/json"}
  
    Rho::Network.get(setshash, url_for( :action => :serversettings_callback ))

But now the body parameter no longer produces the returned JSON, even though I can debug and see it in the @response string, the body parameter shows blank.  I tried both:

    returnparams = @params['body']

and
        returnparams = Rho::JSON.parse(@params['body'])

but no luck.  Here is the string contained within @params:

{
     "rho_callback"=>"1",
     "body"=>"{
          \\"defaultState\\":\\"PA\\",
          \\"defaultlocationsettingid\\":1,
          \\"emaname\\":\\"Berks County\\",
          \\"username\\":\\"Robert Richard\\",
          \\"authorized\\":\\"true\\",
          \\"userisstate\\":\\"false\\",
          \\"userisfema\\":\\"false\\"
     }",
     "cookies"=>".ASPXAUTH=;path=/;",
     "http_error"=>"200",
     "status"=>"ok",
     "headers"=>{
          "cache-control"=>"private",
          "content-length"=>"241",
          "content-type"=>"application/json; charset=utf-8",
          "date"=>"Thu, 26 Sep 2013 17:54:18 GMT",
          "server"=>"Microsoft-IIS/7.5",
          "set-cookie"=>".ASPXAUTH=; expires=Tue, 12-Oct-1999 04:00:00 GMT; path=/; HttpOnly",
          "x-aspnet-version"=>"4.0.30319",
          "x-aspnetmvc-version"=>"4.0",
          "x-powered-by"=>"ASP.NET"
     }
}

And the raw JSON string returned from the server as observed by a browser is:

{"defaultState":"PA","defaultlocationsettingid":1,"emaname":"Berks County","username":"Robert Richard","authorized":"true","userisstate":"false","userisfema":"false"}

So what is the answer to get this to work properly???

Submitted by 830f7cad-018f-… on June 08, 2013 Permalink

Ok, so after trying every combination I could think of to resolve the issue (further researching Rho::JSON.parse, reformatting the JSON string, escaping the slashes, removing segments of code, adding segments of code, and so on...), I finally came up with the right combination.  Here is the code that works, for those of you pulling your hair out to figure this out.  EVEN THOUGH the body is JSON format, you still need to use Rho::JSON.parse, contrary to the old school of thought where JSON data would automatically be parsed without the need to use Rho::JSON.parse (see old code is first post above).  New code should resemble:

    setshash = Hash.new
    setshash['url'] = @appconfig.mainserverurl + "AppConfig/GetServerSettings?useremail=" + @appconfig.useremail + "&password=" + @appconfig.password
    Rho::Network.get(setshash, url_for( :action => :serversettings_callback ))

And then...

  def serversettings_callback
    if @params["status"] == "ok"
    
      #returnparams = @params['body']
      returnparams = Rho::JSON.parse(@params['body'])
  
      if returnparams["authorized"] != "true"
         ... rest of code below...

Hope this helps someone else out there...