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.
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.
Related
I'm sending a Map from a server to a Android app as a JSON file. Previously I was sending Arrays, Strings, Booleans, and Longs which were decoded by null checks and
json.getJSONArray(key)
or similar, where json is a JSONObject and key is a String.
.getJSONMap(key) does not exist and other similar questions do not provide answers that include the case where the JSON has other data and needs a key to specify where the map is.
What can I use to parse the JSON to a map given a key?
You want json.getJSONObject(key)
As per my understanding, we can use Base.64 to encode a string or map to string. But I am facing trouble here, converting the map to string isn't working its giving empty JSON.
So is there any way we can directly encode Map<String,Object> to String value either by using Base64 or converting map to string value.
Base64.getEncoder().encodeToString(“actualString”.getBytes());
I tried converting Map to String. It isn't working.
Now, instead of string, I want to pass a map. Please suggest some single line optimized code to encode Map<String,Object> to String value.
Base64.getEncoder().encodeToString(String.format("string1","string2","string3").getBytes());
First look at How to convert hashmap to JSON object in Java.
Then you can convert the resulting JSON to a base64 encoding.
GSON or Jackson are good libraries for JSON manipulation.
If you want to convert your map to string, getting only the values, you could do something like this:
String mapConverted = map.entrySet().stream()
.map(e -> e.getValue())
.collect(Collectors.joining(","));
In this case, the string mapConverted, contains all values of your map separated with a comma.
See following link. In Java How to Convert Map / HashMap to JSONObject? [4 Different Ways]
https://crunchify.com/in-java-how-to-convert-map-hashmap-to-jsonobject-4-different-ways/
Please check the below link where it show how to create Map from java POJO class using Jackson API in generic way and same can be used to convert from Map to String also.
https://www.thetechnojournals.com/2019/10/how-to-convert-java-object-to-map-or.html
In my code I have declared this data structure below:
LinkedHashMap<String, TreeMap<Integer, LinkedHashMap<String, String>>> GroupsOfaSignature = new LinkedHashMap<String, TreeMap<Integer, LinkedHashMap<String, String>>>();
I want to save it in redis which has its own types of data structures(Strings,hash,...) .In the redis documentation they said that we can store hashmap Where the key and the values are Strings so my question is if i can store a hashmap where the keys or values are not strings,for example a linkedhashMap like the example above
You need to convert them as strings. You can either use some Json libraries like gson to give you the equivalent Json string or you can use message pack to achieve the same.
http://msgpack.org/index.html
Hope this helps
You have to convert them to String though you can also store bitmaps . I would say when you have to store such a a representation either prefix key with some unique identifier for all the internal maps or use Json representation
You can also think of a Redis hash as a JSON object (with non-nestable objects).
Key: "usernameToUidMapping:a"
Value: "alpha" => "1"
"adam" => "312"
"acrobat" => "333"
"aromatic" => "664"
Refer below link
Redis Data Types
I'm trying to parse data obtained via Apache HTTPClient in the fastest and most efficient way possible.
The data returned by the response is a string but in a CSV like format:
e.g. the String looks like this:
date, price, status, ...
2014-02-05, 102.22, OK,...
2014-02-05, NULL, OK
I thought about taking the string and manually parsing it, but this may be too slow as I have to do this for multiple requests.
Also the data returned is about 23,000 lines from one source and I may have to parse potentially several sources.
I'm also storing the data in a hash map of type:
Map<String, Map<String, MyObject>>
where the key is the source name, and value is a map with the parsed objects as a key.
So I have 2 questions, best way to parse a 23,000 line file into objects, and best way to store it.
I tried a csv parser, however the double's if not present are stored as NULL and not 0 so I will need to manually parse it.
Thanks
I am looking for suggestions on how best to send multimap values from java to javascript in json format. Besides using an object with a key field and an arraylist, are there any other options I could explore to send multimap values in json format for processing in javascript on the browser? thanks in advance
If you intend on processing all the values you might just want to send it as a list of pairs.
Otherwise then that map to lists is your friend.
Some code for clarity.
// list of pairs
[["key1", "val11"],
["key1", "val12"] .....]
// map to lists
{ "key1": ["val11", "val12"], ...... }