Home > Blockchain >  Extra new line at the end of json variable
Extra new line at the end of json variable

Time:11-16

I have a string

3e8c837f-907e-46dd-9f71-697ccbb065d6

which I encode to base64. After encoding via Base64.encodeToString(this.toByteArray(Charsets.UTF_8), Base64.DEFAULT) the output is equal to:

M2U4YzgzN2YtOTA3ZS00NmRkLTlmNzEtNjk3Y2NiYjA2NWQ2

Everything is fine but when I put this String to JSONObject() like that:

val json = JSONObject()
json.put("id", encodedId)

The id value in sended json looks like:

"id":"M2U4YzgzN2YtOTA3ZS00NmRkLTlmNzEtNjk3Y2NiYjA2NWQ2\n"

As you can see there is extra

\n

. What can be wrong?

CodePudding user response:

The \n there is a characteristic of the Base64.DEFAULT in android. If you want to remove it, you can replace it with Base64.NO_WRAP.

Base64.encodeToString(this.toByteArray(Charsets.UTF_8), Base64.NO_WRAP)
  • Related