3

I have this API that returns a list as a response:

https://animension.to/public-api/search.php?search_text=&season=&genres=&dub=&airing=&sort=popular-week&page=2

This returns a response string in list format like this

[["item", 1, "other item"], ["item", 2, "other item"]]

How do I convert this so that I can use it as a list, not a string data type?

HoRn
  • 1,458
  • 5
  • 20
  • 25
Laww
  • 33
  • 3
  • Could this be useful for you? https://stackoverflow.com/questions/3413586/string-to-string-array-conversion-in-java – HoRn Aug 27 '22 at 13:24
  • 1
    I would deserialize it using [kotlinx.serialization.json](https://github.com/Kotlin/kotlinx.serialization) – Matt Groth Aug 27 '22 at 14:01
  • The problem is, that some of the strings in the api response contain comma symbols (e.g., `Kanojo, Okarishimasu 2nd Season`) which denies the use of `.split(",")`. Otherwise this would be a simple task – yezper Aug 27 '22 at 14:17
  • If you could get this response in json format you could use a serialization library such as kotlinx.serialization – yezper Aug 27 '22 at 14:18
  • @MattGroth unfortunately the response is not application/json – yezper Aug 27 '22 at 14:18
  • You need an advanced regex for this task, but right now I do not have the time to go about it sorry. – yezper Aug 27 '22 at 14:23
  • 1
    @inoflow the raw string displayed in the link from the original question is properly formatted json. Deserializing the raw string should work. – Matt Groth Aug 27 '22 at 14:27

1 Answers1

1

One way is to use Kotlinx Serialization. It doesn't have direct support for tuples, but it's easy to parse the input as a JSON array, then manually map to a specific data type (if we make some assumptions).

First add a dependency on Kotlinx Serialization.

// build.gradle.kts

plugins {
  kotlin("jvm") version "1.7.10"
  kotlin("plugin.serialization") version "1.7.10" 
  // the KxS plugin isn't needed, but it might come in handy later!
}

dependencies {
  implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.0")
}

(See the Kotlinx Serialization README for Maven instructions.)

Then we can use the JSON mapper to parse the result of the API request.

import kotlinx.serialization.json.*

fun main() {
  val inputJson = """ 
     [["item", 1, "other item"], ["item", 2, "other item"]]
  """.trimIndent()

  // Parse, and use .jsonArray to force the results to be a JSON array
  val parsedJson: JsonArray = Json.parseToJsonElement(inputJson).jsonArray

  println(parsedJson)
  // [["item",1,"other item"],["item",2,"other item"]]
}

It's much easier to manually map this JsonArray object to a data class than it is to try and set up a custom Kotlinx Serializer.

import kotlinx.serialization.json.*

fun main() {
  val inputJson = """ 
     [["item", 1, "other item"], ["item", 2, "other item"]]
  """.trimIndent()

  val parsedJson: JsonArray = Json.parseToJsonElement(inputJson).jsonArray

  val results = parsedJson.map { element ->
    val data = element.jsonArray
    // Manually map to the data class. 
    // This will throw an exception if the content isn't the correct type...
    SearchResult(
      data[0].jsonPrimitive.content, 
      data[1].jsonPrimitive.int,
      data[2].jsonPrimitive.content,
    )
  }

  println(results)
  // [SearchResult(title=item, id=1, description=other item), SearchResult(title=item, id=2, description=other item)]

}

data class SearchResult(
  val title: String,
  val id: Int,
  val description: String,
)

The parsing I've defined is strict, and assumes each element in the list will also be a list of 3 elements.

aSemy
  • 5,485
  • 2
  • 25
  • 51