{"id":2167,"date":"2022-08-30T15:22:36","date_gmt":"2022-08-30T15:22:36","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/04\/pass-javascript-object-to-javas-jsonobject-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:22:36","modified_gmt":"2022-08-30T15:22:36","slug":"pass-javascript-object-to-javas-jsonobject-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/pass-javascript-object-to-javas-jsonobject-collection-of-common-programming-errors\/","title":{"rendered":"Pass javascript object to java&#39;s JsonObject-Collection of common programming errors"},"content":{"rendered":"<p>I am trying to pass some simple javascript object from my Parse.com cloud function (back-end)to my android application.<\/p>\n<p>Here is the code of the javascript class:<\/p>\n<pre><code>function Rate()\n{\n  this.avgRate = 0.0;\n  this.numberOfRaters = 0;\n}\n<\/code><\/pre>\n<p>Here is how I pass it (2 options):<\/p>\n<pre><code>response.success(resp); \/\/ {avgRate = 4,numberOfRaters = 1}\nresponse.success(JSON.stringify(resp)); \/\/ {\"avgRate\":4,\"numberOfRaters\":1}\n<\/code><\/pre>\n<p>Here is how I get it in java:<\/p>\n<pre><code>Object test = ParseCloud.callFunction(\"createNewRate\", params); \/\/ test is {avgRate = 4,numberOfRaters = 1} or {\"avgRate\":4,\"numberOfRaters\":1} (option 1 or 2)\nJSONObject response = ParseCloud.callFunction(\"createNewRate\", params); \/\/ here is a crash\n<\/code><\/pre>\n<p>The error that I get is:<\/p>\n<pre><code>06-16 23:51:44.337: E\/AndroidRuntime(13892): Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to org.json.JSONObject\n<\/code><\/pre>\n<p>How should I handle this correctly?<\/p>\n<p>Thanks<\/p>\n<p id=\"rop\"><small>Originally posted 2014-01-04 02:50:37. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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)); \/\/ {&#8220;avgRate&#8221;:4,&#8221;numberOfRaters&#8221;:1} Here [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2167","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2167"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2167\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}