Sidebar

Why do I get this error: Java class "[B" has no public instance field or method named "toJSON"

0 votes
9.6K views
asked Mar 4, 2014 by rich-c-2789 (17,490 points)

Why do I get this error: Java class "[B" has no public instance field or method named "toJSON".  

I am trying to create json with values captured from calling some QIE apis found in the code wizard.  When I call JSON.stringify I get:

Java class "[B" has no public instance field or method named "toJSON".

 

1 Answer

0 votes

This error is usaually the result of using a Java object or string where a javascript string is expected.  The code wizard apis return java object/strings that need to be converted to JavaScript strings before being used in json.  To convert a string add an empty string ('').  Compare the differences in these three examples:

 

//Success
var myJavaScriptStringDate = '2014-02-03';
var somejson = {date:myJavaScriptStringDate};
JSON.stringify(somejson);

//Success
var myConvertedToJavaScriptString = '' + qie.formatDate('yyyy-mm-dd', new java.util.Date(qie.deduceDate(qie.getSystemDate()).getTime()));
somejson = {date:myConvertedToJavaScriptString};
JSON.stringify(somejson);

//Fails
var myJavaStringDate = qie.formatDate('yyyy-mm-dd', new java.util.Date(qie.deduceDate(qie.getSystemDate()).getTime()));
somejson = {date:myJavaStringDate};
JSON.stringify(somejson);

The first example is just a plain JavaScript string.  The second is converted to JavaScript.  The last one fails because the api returns a Java string that is not converted.  The last two example are the same except for the empty string that forces it to be converted.

Also see this question:

https://www.qvera.com/kb/index.php/1/why-doesnt-my-string-have-a-replaceall-function?show=1#q1

 

answered Mar 4, 2014 by rich-c-2789 (17,490 points)
...