简体   繁体   中英

How to Convert A String to Object In Kotlin

I have a string as following:

IngestDataRequestDto(initiatorEmail=sample@sample.com, ingestionDetails=IngestionDetails(selectedColumns={...

I want to convert this to IngestDataRequestDto.kt data class.

I tried to use deserilize with jackson and also direct casting but both not worked

  try {
      previousValue = SerializationService.deserialize(sample.previousValue, IngestDataRequestDto::class)
    } catch (e: Exception) {
      previousValue = sample.previousValue as IngestDataRequestDto
    }

try gives jackson error, catch string to object exception

That string seems to have been generated by a toString() call on some IngestDataRequestDto object, so it is not properly formatted as a Json or anything like that and, as such, can't be deserialized using these steps.

For this task, you'll need to make your own implementation of a deserializer, maybe using regex or, if that string always follows that format, you can make a template to extract the information you need.

Even though, this is definitely not the best way to do this and you should search for what generated that string in the first place and check if there's a better way to get that object's data.

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