Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
 
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"]] –  sdouglass Feb 8 at 6:30
 
it looks like [["a", "b", "c"],["x"],["y","z"]] –  AlwaysALearner Feb 8 at 6:40
 
@user1649068 you want to create a array of java objects from jsonarray? –  Parvathy Feb 8 at 12:32
 
Yes @Parvathy.. –  AlwaysALearner Feb 8 at 13:59
add comment

2 Answers

up vote 1 down vote accepted

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;
}
share|improve this answer
add comment

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()]);
share|improve this answer
 
According to my question, what is JSONObject here? jsonArray or jsonArrayStr? –  AlwaysALearner Feb 8 at 5:01
 
jsonArray ... –  TheWhiteRabbit Feb 8 at 5:02
 
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? –  AlwaysALearner Feb 8 at 5:05
1  
yes, directly pass the jsonArray as request parameter to jsp2 and in jsp2 do as mentioned above :) –  TheWhiteRabbit Feb 8 at 5:06
 
are you talking about net.sf.json.JSONArray –  AlwaysALearner Feb 8 at 5:24
show 7 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.