Kotlin如何将每一行保存成Class个对象?
Kotlin how to save each line into Class Objects?
我收到来自 API 的列表。我把它按行划分了。我不确定如何将每一行保存到 class 个对象中?你可以帮帮我吗?
[在此处输入图片描述][1]
class RecordsList {
@RequestMapping("/")
fun receiveAll(): List<String>? {
val restTemplate = RestTemplate()
val url = "some URL // doesn't matter"
val response = restTemplate.getForObject(url, String::class.java)
var lines = response?.lines()
lines?.forEach { line -> println(line)}
return lines
}
}
data class Record(var domain: String, var code: String, var link: String, var other: String)
您应该创建一个变量来存储记录的数组列表。
class RecordsList {
@RequestMapping("/")
fun receiveAll(): ArrayList<Record>? {
// ArrayList to store the `Record`s
var arrayListOfRecord: ArrayList<Record>? = arrayListOf()
val restTemplate = RestTemplate()
val url = "some URL // doesn't matter"
val response = restTemplate.getForObject(url, String::class.java)
var lines = response?.lines()
// For each line in the response create a Record object and add it to the
// `arrayListOfRecord`s
lines?.forEach { line ->
arrayListOfRecord?.add(
Record(
domain = line.domain,
code = line.code,
link = line.link,
other = line.other
)
)
}
return arrayListOfRecord
}
}
data class Record(var domain: String, var code: String, var link: String, var other: String)
我收到来自 API 的列表。我把它按行划分了。我不确定如何将每一行保存到 class 个对象中?你可以帮帮我吗? [在此处输入图片描述][1]
class RecordsList {
@RequestMapping("/")
fun receiveAll(): List<String>? {
val restTemplate = RestTemplate()
val url = "some URL // doesn't matter"
val response = restTemplate.getForObject(url, String::class.java)
var lines = response?.lines()
lines?.forEach { line -> println(line)}
return lines
}
}
data class Record(var domain: String, var code: String, var link: String, var other: String)
您应该创建一个变量来存储记录的数组列表。
class RecordsList {
@RequestMapping("/")
fun receiveAll(): ArrayList<Record>? {
// ArrayList to store the `Record`s
var arrayListOfRecord: ArrayList<Record>? = arrayListOf()
val restTemplate = RestTemplate()
val url = "some URL // doesn't matter"
val response = restTemplate.getForObject(url, String::class.java)
var lines = response?.lines()
// For each line in the response create a Record object and add it to the
// `arrayListOfRecord`s
lines?.forEach { line ->
arrayListOfRecord?.add(
Record(
domain = line.domain,
code = line.code,
link = line.link,
other = line.other
)
)
}
return arrayListOfRecord
}
}
data class Record(var domain: String, var code: String, var link: String, var other: String)