Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am getting a json like string which have = symbol instead of :. as shown below

{reply={messageId=4.30486913E8, mimeType=image/jpeg, sender=panipaali@gmail.com, type=file, thumbnail=/9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAAB
AAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAIKAD
AAQAAAABAAAAKwAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklN
BCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+/8AAEQgAKwAgAwEiAAIRAQMRAf/EAB8A
AAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAAB
fQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYn
KCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeI
iYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh
4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYH
CAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRC
kaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZX
WFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKz
tLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/bAEMA
AgICAgICBAICBAYEBAQGCAYGBgYICggICAgICgwKCgoKCgoMDAwMDAwMDA4ODg4O
DhAQEBAQEhISEhISEhISEv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gIEccjxrjdjkIwB/GvMvAPw08Dy+PnEunq4d3kYM7kMy
hQMgtg8djxWOMw9v30nc9DCVeZ+xirXPgDwn8CPG/hbwM+teErHUptTudjCWCUIY
gDlij4AfptbHrjkVk3sus+I7GLQfiZqYstctjKIXkhRUWORFCwTzJtVWcr1IIx94
54r+gPR/DGg6eoisbcRJ5f3VLAdfTNY3xC+HfgrxN4Yu9F13Tori1ugDKhyNxRty
kkEHIPIOa8GGIqqfNLZP59uv/APVq4Cm17rf6H//2Q==}}

I used the GSON library for parsing this string by using the below code

data class ChatReplyDTO(
    val messageId: Long,
    val message: String?,
    val sender: String,
    val mimeType: String?,
    val type: String?,
    val thumbnail: String?
)

data class ChatReplyObjectDTO(val reply: ChatReplyDTO)
val classType = object : TypeToken<ChatReplyObjectDTO>() {}.type
val reply = Gson().fromJson<ChatReplyObjectDTO>(dataFormatted, classType)

But error happening in forward slash of mimeType=image/jpeg. So I replaced it with an empty string for testing. Then there is another error introduced by thumbnail value. If I remove mimeType and thumbnail key and value from the string, it perfectly worked by setting null in those fields in the DTO object and other fields are filled with correct values. I referred to another StackOverflow solutions which has several strings replace functionality and nothing worked. Do we have any simple library like Gson to resolve this problem?

One important thing is, messageId and sender are mandatory. All other fields might not be there with other json.

question from:https://stackoverflow.com/questions/65930852/json-like-string-to-json-in-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
67 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...