2

I have a JavaScript JSON array[array[String]] called jsonArray in my JSP1.jsp.

I am converting jsonArray to a String jsonArrayStr using JSON.stringify(jsonArray) in JSP1.jsp.

I am passing jsonArrayStr as a parameter while calling another JSP JSP2.jsp this way-

"JSP2.do?jsonArrayStr="+jsonArrayStr

In JSP2.jsp, I am doing this-

String jsonArrayStr = request.getParameter("jsonArrayStr");

Now how do I convert jsonArrayStr to Java array (JSP2.jsp doesn't contain any JavaScript code)

Summary-

I have a JavaScript JSON Array in a JSP1.jsp, which I want to access as a normal Java array/arraylist in JSP2.jsp. How do I achieve this?

3
  • Are you starting out with a one-dimensional array, or a two-dimensional array? Does your array contain just Strings, or does it contain arrays that contain strings? JSON.stringify() on e one-dimensional array would look like: ["a", "b", "c", "x", "y", "z"] JSON.stringify() on a two-dimensional array would look like: [["a", "b", "c"],["x"],["y","z"]] Commented Feb 8, 2013 at 6:30
  • it looks like [["a", "b", "c"],["x"],["y","z"]] Commented Feb 8, 2013 at 6:40
  • @user1649068 you want to create a array of java objects from jsonarray? Commented Feb 8, 2013 at 12:32

2 Answers 2

1

OK, so you have a two-dimensional array of strings represented as a JSON like this stored in a Java String:

[["a", "b", "c"],["x"],["y","z"]]

You need to somehow parse or "deserialize" that value into a Java String[][]. You can use a library like from http://www.json.org/java/index.html or http://jackson.codehaus.org/ or you can try to do it manually. Manually could be a little tricky but not impossible. The json.org library is very simple and might be good enough. The code would be something like this (I haven't tried/tested this):

JSONArray jsonArray = new JSONArray(jsonArrayStr); // JSONArray is from the json.org library
String[][] arrayOfArrays = new String[jsonArray.length()][];
for (int i = 0; i < jsonArray.length(); i++) {
    JSONArray innerJsonArray = (JSONArray) jsonArray.get(i);
    String[] stringArray = new String[innerJsonArray.length()];
    for (int j = 0; j < innerJsonArray.length(); j++) {
        stringArray[j] = innerJsonArray.get(j);
    }
    arrayOfArrays[i] = stringArray;
}
0

somethingk like this

ArrayList<String> strings= new ArrayList<String>();     
JSONArray jsonArray = (JSONArray)jsonObject; 
if (jsonArray != null) { 
   int len = jsonArray.length();
   for (int i=0;i<len;i++){ 
    list.add(jsonArray.get(i).toString());
   } 
} 

String[] Stringarray = strings.toArray(new String[strings.size()]);
9
  • According to my question, what is JSONObject here? jsonArray or jsonArrayStr? Commented Feb 8, 2013 at 5:01
  • So, u mean to say there is no need for me to stringify() the json object? I will be able to directly access the JSONobject across JSP pages without passing them? Commented Feb 8, 2013 at 5:05
  • 1
    yes, directly pass the jsonArray as request parameter to jsp2 and in jsp2 do as mentioned above :) Commented Feb 8, 2013 at 5:06
  • are you talking about net.sf.json.JSONArray Commented Feb 8, 2013 at 5:24
  • This is how I am passing the array value to another JSP as u said- "JSP2.do?jsonArray="+jsonArray; Commented Feb 8, 2013 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.