I am getting the following error when passing JSON data from a javascript function to a bean.
***java.lang.String cannot be cast to org.json.simple.JSONArray***
.
I am using json-simple to parse the data the in the backend
I am using an a4j Js Function provided by rich faces to pass the JSON data as an actionParam.
I am using simple-json to parse and decode the json data that is being passed.
Configurations
richfaces 3.3.3
jsf v1.2
apache tomcat v7.0.x
simple-json
Here is my javascript variable that i am sending to the bean
var a = [
{
'name': 'John',
'height' : '170',
'age' : 26
},{
'name': 'Doe',
'height' : '180',
'age' : 30
}
];
removeServiceGroup(JSON.stringify(a));
This the a4j:jsfunction that I use to call the back end bean
<a4j:jsFunction name="removeServiceGroup" action="#{someBean.removeServiceGroup}">
<a4j:actionparam name="jsonData" />
</a4j:jsFunction>
In the bean this is how i receive the data and parse it
String a = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("jsonData");
System.out.println(a);
JSONParser parser = new JSONParser();
Object obj = parser.parse(a);
JSONArray arr = (JSONArray)obj;
the system.out.println displays this on the console
"[{\"name\": \"John\", \"height\": \"170\", \"age\": 26}, {\"name\": \"Doe\", \"height\": \"180\", \"age\": 30}]"
what am i doing wrong. I went through several forums and tried doing this in several different methods, but it all ends up with the same error message
java.lang.String cannot be cast to org.json.simple.JSONArray