0

I would like to decode in base64 a string to an object:

this is a snippet how I decode:

byte[] asBytes = Base64.getDecoder().decode("ew0KCSJ1cmwiOiAibXlVcmwub3JnL3Byb2R1Y3RzIiwNCgkibnVtIjogMTI1OTY1NA0KfQ==");

the encoded string contain this object:

{
    "url": "myUrl.org/products",
    "num": 1259654
} 

I need to do something like that:

MyObjectWrapper mObj = asByte.somthing_...

best regards

Victor
  • 385
  • 2
  • 12
  • 26
  • 2
    If this is a JSON encoded object, why don't you using an open-source JSON parser, like GSON (https://github.com/google/gson)? – Sam Goldberg May 29 '17 at 09:24
  • 2
    So, you know how to get the bytes. Do you know how to convert the bytes to a string? You need to know the encoding used - it's likely to be UTF-8. Once you've performed that conversion, you need to parse the JSON as an object - do you know how to do that? Basically, these are three separate conversions: please be specific about which one you're confused about. – Jon Skeet May 29 '17 at 09:25
  • Try to check this class `java.io.ObjectInputStream` – Roman Proshin May 29 '17 at 09:25
  • Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Tom May 29 '17 at 09:37
  • Mybe I wasn't clear in my question. What I want to do is not parsing JSON but convert **byte[]** to an object – Victor May 29 '17 at 09:40
  • Which ends up parsing that byte[] as what it is: JSON. – Tom May 29 '17 at 09:46

1 Answers1

3
    com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
    String str = new String(asBytes, Charset.forName("UTF-8"));
    MyObjectWrapper mObj = mapper.readValue(str, MyObjectWrapper.class);
StanislavL
  • 56,971
  • 9
  • 68
  • 98