0

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
BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • This is very bizarre, I just tested the same code as yours and it's working very fine. Are you sure this is really the value that is causing the error ? I just executed that code and it gave me: `Array: [{"height":"170","age":26,"name":"John"},{"height":"180","age":30,"name":"Doe"}]` as output, `arr.toString()` – Laabidi Raissi Jun 13 '13 at 19:42
  • Did you create the String in Java and try the code on it or was the data passed from a javascript function on the client side. Cuz when i tried the first approach(create the string in java) , it works. The difference here looks like the data passed from the javascript has '\' which Java doesnt seem to parse. It takes it literally. Also i notices that the String sent from the javascript begins with a Double Quotes when displayed on the console, unlike the String that was created in the Java – user2317577 Jun 13 '13 at 20:42
  • ouups, yes I just used that `String` as it is. Please see my answer – Laabidi Raissi Jun 13 '13 at 21:14
  • now this is what i don understand. Why would i have to format the input this way?. Shouldnt it be handled by itself instead of me manually having it to eliminate these extra charectrers? I am not usre if the issue here is with 'Stringify' or if its with the 'Simple JSON' that I am using – user2317577 Jun 19 '13 at 14:52

1 Answers1

0

Based on your comment, there seems to be a problem transferring JSON data using a4j:actionparam.
A workaround would be to parse the value of request param manually to remove extra things before passing it to JSONParser:

input = input.replace("\\\"", "\"");//Remove extra \
if(input.startsWith("\"")){ //Remove " at starting of the value
input = input.substring(1);
}
if(input.endsWith("\"")){//Remove ending " 
    input = input.substring(0,input.length()-1);
}

Also please check this forum post and this wiki page.

Laabidi Raissi
  • 3,263
  • 1
  • 22
  • 28