Java : 64 based string decode / parse failed

177 views Asked by At

I am trying to convert this 64 based encoded JSON string and convert received JSON into POJO using flexjson API.

First try block, converts direct JSON as string into object which is success. This string is decoded using online tool.

Now second try block, try to convert 64 based string into an object in a similar way but converting the 64based string on the run which is throwing exception flexjson.JSONException: Expected a ',' or ']' at character 10

 try {
        AsyncResponseDO asyncResponseDO = new JSONDeserializer<AsyncResponseDO>().deserialize("{\"relatesTo\":\"7_Sept2017_IF01\"}", AsyncResponseDO.class);
        System.out.println(asyncResponseDO.getRelatesTo());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

try {
        AsyncResponseDO asyncResponseDO = new JSONDeserializer<AsyncResponseDO>().deserialize(Base64.decodeBase64("eyJyZWxhdGVzVG8iOiI3X1NlcHQyMDE3X0lGMDEifQ==".getBytes()).toString(), AsyncResponseDO.class);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

POJO class :

public class AsyncResponseDO {
private String relatesTo; 

public String getRelatesTo() {
    return relatesTo;
}
public void setRelatesTo(String relatesTo) {
    this.relatesTo = relatesTo;
}
}
1

There are 1 answers

0
Gaurav Saraf On BEST ANSWER

new String(Base64.decodeBase64("eyJyZWxhdGVzVG8iOiI3X1NlcHQyMDE3‌X0lGMDEifQ==".getByt‌es()));

This will convert into a proper string.

I referred to https://www.mkyong.com/java/how-do-convert-byte-array-to-string-in-java/