0

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());
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
  • 1
    Please [edit] your question to include the code that is converting a map to empty JSON, along with some sample input. A [mcve] would be ideal. – azurefrog Oct 25 '19 at 18:56
  • @ArvindKumarAvinash Instead of "actualString" I want to use Map –  Oct 25 '19 at 19:02
  • @ArvindKumarAvinash but putAll is not working JSONObject json = new JSONObject(); json.putAll( data ); –  Oct 25 '19 at 19:07
  • @ArvindKumarAvinash any difference between 4 and 36 radix value? –  Oct 25 '19 at 19:32

4 Answers4

0

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.

fedup
  • 1,209
  • 11
  • 26
0

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.

Juliano Pacheco
  • 521
  • 1
  • 5
  • 13
0

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/

hbi640
  • 11
  • 2
  • Welcome to SO! When you post an answer based in a Live Link, there is some risk that the answer will become useless if the links disappear. Please Explain a little bit what can be found there so your reply will be useful. – David García Bodego Nov 05 '19 at 03:28
0

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

Ashok Prajapati
  • 374
  • 2
  • 7