Uncaught Monkey Exception when creating jsonobject

Monkey Forums/Monkey Programming/Uncaught Monkey Exception when creating jsonobject

drackbolt(Posted 2014) [#1]
Hi all,

I'm checking out Monkey again and trying to do something I thought would be simple: I'm trying to consume JSON from a rest layer I built in another engine. I can get it, can see the JSON, and can copy and paste it into values and create a new JsonObject successfully. But what I cannot do is create a new JsonObject with the direct output from the rest layer.

In other words, if I have a successful HTTPRequest and I put the JSON returned into a string "response", it blows up and gives me only "Uncaught Monkey Exception" on this line:
Local jsonObj:= New JsonObject(HTTPreq.response)

I've tried replacing newlines and returns (~n, ~r) in the string and cutting out some characters, with no luck.

You can use this online sample for some returned JSON (not mine): http://echo.jsontest.com/key/value/one/two
It errors in the same way as my own attempts.

If I make a local string that contains the same return text as I've typed it, no problem. the object is created perfectly.

I've added a custom content-json header to the writer in my rest layer, including enforcing UTF-8, but it hasn't made a difference.

Anyone else have this issue?

Thanks
-Jesse


CopperCircle(Posted 2014) [#2]
I use json all the time from HTTP req's and don't have any problems, try putting the req response into a string first and print it to the console to check it looks ok. also remove any extra spaces in the json string and try .Trim().


Trez(Posted 2014) [#3]
I tested the Url provided without any problems:

Import mojo
Import brl.json
Import brl.httprequest

Function Main()
	New Game()
End

Class Game Extends App Implements IOnHttpRequestComplete

	Method OnCreate()
	
		SetUpdateRate(60)
		
		Local httpReq:HttpRequest
		
		httpReq = New HttpRequest("GET", "http://echo.jsontest.com/key/value/one/two", Self)
		httpReq.Send()
	
	End
	
	Method OnHttpRequestComplete:Void(req:HttpRequest)
		
		Print("Http Response :" + req.ResponseText)
		
		Local jsonObj:JsonObject = New JsonObject(req.ResponseText)
		
		Print(jsonObj.Get("one").StringValue())		
		Print(jsonObj.Get("key").StringValue())
		
	End Method
	
	Method OnUpdate()
		UpdateAsyncEvents()
	End
	
	Method OnRender()
		Cls()
		
	End

End



drackbolt(Posted 2014) [#4]
Thanks so much for the replies.

The code above worked for me also, so I dug deeper, and found that where it fails to create the JsonObject is when it's outside of the OnHttpRequestComplete method. If I try to pass the response string as a member to logic in the Update or Render areas, it fails with the above error. Is that expected?

Strict
Import mojo
Import mojo.app
Import mojo.graphics
Import brl.httprequest
Import brl.json

Global HTTPreq:= New HTTPreqClass
 
Function Main:Int()
       New MyApp
 	
       Return 0
End
 
Class MyApp Extends App
	Method OnCreate:Int()
			SetUpdateRate 60
			
			HTTPreq.GetTxtFromWeb("http://echo.jsontest.com/key/value/one/two")

			Return 0
	End

	Method OnUpdate:int()

		If KeyHit( KEY_CLOSE ) Error ""

		  If HTTPreq <> Null
			  Local jsonObj:= New JsonObject(HTTPreq.response)  ''This fails every time
		  EndIf

		UpdateAsyncEvents
		Return 0
	End


       Method OnRender:Int()
              Cls (0,0,200)
			  DrawText(HTTPreq.response, 200, 200)
			  
		  Return 0
       End Method
End

Class HTTPreqClass Implements IOnHttpRequestComplete

	Field get_req:HttpRequest, post_req:HttpRequest
	Field jsonObj:JsonObject
	Field response:String
	
	Method OnHttpRequestComplete:Void(req:HttpRequest)
	
		If req=get_req
			Print "Http GET complete!"
		Else
			Print "Http POST complete!"
		Endif

		Print "Status=" + req.Status()
		response = req.ResponseText()
		jsonObj = New JsonObject(response)  ''This works fine

	End
	
	Method GetTxtFromWeb:Void(path:String)

		get_req = New HttpRequest( "GET", path, Self )
		get_req.Send()
	End
	
End


Thanks again.


muddy_shoes(Posted 2014) [#5]
It fails because your logic check doesn't stop the response being used before it has actually returned:

 If HTTPreq <> Null 


HTTPreq is never Null as you create it on app construction. You probably want to use

 If HTTPreq.response <> "" 


or

 If HTTPreq.post_req <> Null 


Or set a flag yourself in the OnHttpRequestComplete method.


drackbolt(Posted 2014) [#6]
Ah makes sense... thanks again!

Been too long since I got into coding again...