how to encode Map<String,Object> to String value in java - java

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

Related

How to parse a JSON map in Java or Kotlin where the map has a key

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)

Parse json string of the underline format to compare the values with another map

I have a JSON string that looks like:
"{\"info\":{\"length\":{\"value\":18},\"name\":{\"value\":\"ABC\"}}}"
say, length and name are attribute names
I have another map (say attributeMap) that (created from the results I retrieve from the database) map has attribute name and attribute value association stored.
I need to be able to parse the string and compare the value an attribute has in the above string with the value returned from the attributeMap. Based on those comparisons, I will need to take some decisions.
In order to do this, I should convert the above string to a format that would help make the above comparison easier and efficient. I don't think I should be writing my own parser to do this. what would a right way to do this?
You should use any JSON Parser, like GSON (Google) (Recommended for simplicity), JACKSON, the simple org.json, or any other..
Then you will get a JSONObject/JSONNode to navigate and do the comparison.
You can find a parsing example here: How to parse JSON in Java

Java reevaluate string as hashmap

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.

Parsing Apache CSV like string into objects

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

In Java, how do I retrieve the data values from the xml and store them in an array?

this is my xml file
<waveform>
<Ivalue>12,13,14,15,16,17,18</Ivalue>
<IIvalue>1,4,15,23,22,44</IIvalue>
</waveform>
<waveform>
<Ivalue>12,13,14,15,16,17,18</Ivalue>
<IIvalue>1,4,15,23,22,44</IIvalue>
</waveform>
here, I know how to retrieve the values by tags but is it possible to store these values into separate int[]?
Thanks
You may use JAXB for extracting tags like Ivalue AS STRING.
To my knowledge it is at least not easy to get it directly as int array (with JAXB)
However, it is easy to split the string using String.split and convert the results with
Integer.parse

Categories