简体   繁体   中英

Java: converting bytearray to string and xml

I have an XML with a "token" field in it, in java it's:

@XmlElement(name = "Token", required = true)
protected byte[] token;

I'm using a UUID to generate the token, so I do:

UUID uuid=UUID.randomUUID();
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
byte[] token = bb.array();   
myXML.setToken(token);

and in the XML I get something like that:

<Token>jvXrf8HvSVq23MiwSbnT+A==</Token>

I need also to send the token to a service that use Json notation, so in another funcion I get the token from the XML object and store it in a POJO:

String sToken = Base64.encode(myXML.getToken());
myPojo.setToken(sToken);

I've checked the string and it's correct, but when I convert myPojo to Json with Gson:

GsonBuilder gb = new GsonBuilder()
  .serializeNulls()
  .setPrettyPrinting()
  .setDateFormat("yyyy-MM-dd HH:mm:ss");
Gson gson = gb.create();
String json=gson.toJson(myPojo);

I get this in the json string representation of the object:

jvXrf8HvSVq23MiwSbnT+A\u003d\u003d

with the two "=" translated in "\=". It's normal? Do the \= code get translated to "=" at the receiving part?

Thank you

Yes, GSON changes = to unicode \=.

you can get details info here:

GSON issue with String

Disable escape HTML and then you are good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM