0

I have a socket server and Im sending information from client to server and back.

Sock servers will only send strings or byte arrays of information.

The information I need to transfer is java hashmap, but to send it, I convert it to a string.

activeUsers = new HashMap<Object, Object>();

socket.send(activeUsers.toString());

upon receiving this this string, I need to reconstruct it as a hashmap.

I have seen examples like this, however, I think this would not work for my case.

For instance:

an item in the hashmap can be while the next item is <string, hashmap> and the items in that hashmap could be a combinations of the two.

I'm used to python where I can say evaluate string as dictionary and it does so.

How can I achieve the same thing in java.

Community
  • 1
  • 1
Cripto
  • 3,581
  • 7
  • 41
  • 65
  • 3
    Helllo. I’m .net developer, but don’t you have JSON serializer in Java? Something like JsonConverter.ToJson (yourHashmap) ? – Jurion Apr 17 '14 at 01:27
  • 1
    Maybe this : http://stackoverflow.com/questions/12155800/how-to-convert-hashmap-to-json-object-in-java – Jurion Apr 17 '14 at 01:28
  • If the values in the hashmap are strings, numbers, or other maps or lists of the same then it will translate very nicely into JSON (and translate back the other way on the other end). – Hot Licks Apr 17 '14 at 01:38

1 Answers1

0

Convert the your HashMap to JSON using the technique given here: How to convert hashmap to JSON object in Java

Then after transferring the string data, convert it back to HashMap using the Gson or Jackson library for Java.

Community
  • 1
  • 1
aa8y
  • 3,854
  • 4
  • 37
  • 62