Click here to Skip to main content
15,918,007 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I was trying to pass an array list of hashmaps via the glassfish webservice, but i kept getting a "class java.util.hashmap nor any of its super class is known to this context"

When I added a toString method to it, i get the values like this
Java
[{id=childNode1, description=This is the google link, type=child, href=www.google.com}, {id=childNode2, description=This is the yahoo link, type=child, href=www.yahoo.com}]


can I from a jsp page compile this to an Arraylist of hashmaps?

Please post if i am unclear
Posted

Make it an Array with key/value pairs. Shouldn't cause to much headaches.
 
Share this answer
 
Comments
boude 23-Nov-11 8:52am    
when i create the list on the server side, i append a hashmap to the arraylist.
This is the backend code


ArrayList list = new ArrayList();

// Use this code to add nodes to the map
System.out.println("Current Node is: " + currentNode);
if (currentNode == 1) {
Map map = new HashMap();
map.put("type", "child");
map.put("id", "childNode1");
map.put("href", "www.google.com");
map.put("description", "This is the google link");
list.add(map);
// End with creating Node

map = new HashMap();
map.put("type", "child");
map.put("id", "childNode2");
map.put("href", "www.yahoo.com");
map.put("description", "This is the yahoo link");
list.add(map);
System.out.println("Size of list is: " + list.size());
System.out.println("Sending back the array List");
}
TorstenH. 23-Nov-11 9:24am    
which part is not understandable? don't trick yourself, make it an Array of Objects. The Object is pretty simple, contains 2 Strings.

Can't get simpler.
Ok. So this is what i came up with to get my values. But now i must use the regex to replace the Square brackes that is created in the string. Is there a way to remove all the Square brackets([/]) only? and not the other nonealphanumeric characters

JavaScript
try {
    webservice.CalculatorService service = new webservice.CalculatorService();
    webservice.Calculator port = service.getCalculatorPort();
    Integer nodeType;
    String[] nodeData = result.split(",");
    ArrayList finalList = new ArrayList();
    ArrayList list = new ArrayList();
    int j = 0;
    for (int i = 0; i <= nodeData.length; i++) {
        if (j < 4) {
            j++;
            if (nodeData[i].contains(".")) {
                list.add(nodeData[i]);
            } else {
           String data = nodeData[i].replaceAll("[^\\p{L}]", " ");
                list.add(data);
            }
        } else {
            j = 0;
            i--;
            out.println("<li id="" + list.get(1) + ""><a href="http://" + list.get(2).toString().trim() + "" target="basefrm">" + list.get(3) + "</a></li>");
            list.clear();
        }
    }
} catch (Exception ex) {
    //out.println("Error " + ex);
}
 
Share this answer
 
Comments
RaviRanjanKr 1-Dec-11 14:19pm    
A suggestion :- its not a good practice to post your feedback or comment as an answer. you can use have a question or Comment.
holy s***.
That is creepy, sorry top say it so clear, but it is.

Let me set up a new answer to make the code-highlighting enabled:

You've got a fine HashMap object with Keys and Values.

Now imagine this HashMap to be an Arraylist of Objects with Key/Value pairs - let's call the Object "KeyValue":

Java
import java.io.Serializable;

public class KeyValue implements Serializable{
	private static final long serialVersionUID = 1L;
	
	private final String strKey;
	private final String strValue;
	
	public KeyValue(String strKey, String strValue){
		this.strKey = strKey;
		this.strValue = strValue;
	}

	public String getKey() {
		return strKey;
	}

	public String getValue() {
		return strValue;
	}

}


Then you'll init a ArrayList and put all HashMap Entries into that ArrayList:

Java
HashMap<string,> oMap = new HashMap<string,>();

/* fancy code */

ArrayList<keyvalue> oList = new ArrayList<keyvalue>();
for (String strKey : oMap.keySet()) {
	oList.add(new KeyValue(strKey, oMap.get(strKey)));
}

/* fancy code */
</keyvalue></keyvalue>



Now that ArrayList is filled with your Objects. You can send that ArrayList to the client, and as the client knows the Object too, he should be able to retrieve the data.
 
Share this answer
 
v2
Comments
TorstenH. 2-Dec-11 4:56am    
I saw 2 typos - have fixed them.
Did all the arrayList and things on the backend and set the response type to a string. I then added the code on the jsp page and it works wonders

JavaScript
java.lang.String result = port.treeList(nodeType);
String[] items = result.split(",");
for (int i = 0; i < items.length; i++) {
    String item = items[i].toString().replace("[", "").replace("]", "");
    out.println(item);
}
 
Share this answer
 
Comments
TorstenH. 3-Dec-11 8:35am    
cool, then mark an answer as "accept" to show that this problem is solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900