Pass javascript object to java's JsonObject-Collection of common programming errors
I am trying to pass some simple javascript object from my Parse.com cloud function (back-end)to my android application.
Here is the code of the javascript class:
function Rate()
{
this.avgRate = 0.0;
this.numberOfRaters = 0;
}
Here is how I pass it (2 options):
response.success(resp); // {avgRate = 4,numberOfRaters = 1}
response.success(JSON.stringify(resp)); // {"avgRate":4,"numberOfRaters":1}
Here is how I get it in java:
Object test = ParseCloud.callFunction("createNewRate", params); // test is {avgRate = 4,numberOfRaters = 1} or {"avgRate":4,"numberOfRaters":1} (option 1 or 2)
JSONObject response = ParseCloud.callFunction("createNewRate", params); // here is a crash
The error that I get is:
06-16 23:51:44.337: E/AndroidRuntime(13892): Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to org.json.JSONObject
How should I handle this correctly?
Thanks