英文:
Convert a String into a List in Kotlin
问题
在Kotlin中,我从服务器收到这个字符串形式的地理坐标列表:
[[45.02498, 7.55163], [45.02527, 7.55167], [45.02556, 7.55172],
[45.02564, 7.55173], [45.02582, 7.55175], [45.02582, 7.55188],
[45.02585, 7.55202], [45.02593, 7.55213], [45.026, 7.55218]]
我该如何从这个字符串创建一个Pair的列表?我看到可以使用正则表达式,但我并没有真正理解如何使用它,尤其是处理起始和结束的方括号。
英文:
In kotlin, I receive from a server this list of geocoordinates as a string:
> [[45.02498, 7.55163], [45.02527, 7.55167], [45.02556, 7.55172],
> [45.02564, 7.55173], [45.02582, 7.55175], [45.02582, 7.55188],
> [45.02585, 7.55202], [45.02593, 7.55213], [45.026, 7.55218]]
How can I create a List of Pairs starting from this string? I've seen that is possible to use Regex, but I've not really understood how to use it, expecially having to deal with a starting and ending square parenthesis.
答案1
得分: 1
这个解决方案实际上非常简单,这是一个代码示例。
字符串已转换为一个JSONArray,通过一个 for
循环可以访问所有元素,提取所需的数据。
val jsonArray = JSONArray(receivedText)
for (i in 0 until jsonArray.length()) {
val pairArray = jsonArray.getJSONArray(i)
val latitude = pairArray.getDouble(0)
val longitude = pairArray.getDouble(1)
Log.d("description","Pair $i - Latitude: $latitude, Longitude: $longitude")
}
英文:
The solution actually was very simple, and this is an instance of code.
The string has been transformed into a JSONArray and through a for
loop it's possible to have access to all the elements, extracting the desired data.
val jsonArray = JSONArray(receivedText)
for (i in 0 until jsonArray.length()) {
val pairArray = jsonArray.getJSONArray(i)
val latitude = pairArray.getDouble(0)
val longitude = pairArray.getDouble(1)
Log.d("description","Pair $i - Latitude: $latitude, Longitude: $longitude")
答案2
得分: 0
尝试这个:
val jsonString = "[[45.02498, 7.55163], [45.02527, 7.55167], [45.02556, 7.55172], [45.02564, 7.55173], [45.02582, 7.55175], [45.02582, 7.55188], [45.02585, 7.55202], [45.02593, 7.55213], [45.026, 7.55218]]"
val withoutBrackets = jsonString.removeSurrounding("[", "]")
val pairs = withoutBrackets.split("], [")
val pairList: List<Pair<Double, Double>> = pairs.map {
val coords = it.removeSurrounding("[", "]").split(", ")
Pair(coords[0].toDouble(), coords[1].toDouble())
}
println(pairList)
英文:
Try this:
val jsonString = "[[45.02498, 7.55163], [45.02527, 7.55167], [45.02556, 7.55172], [45.02564, 7.55173], [45.02582, 7.55175], [45.02582, 7.55188], [45.02585, 7.55202], [45.02593, 7.55213], [45.026, 7.55218]]"
val withoutBrackets = jsonString.removeSurrounding("[", "]")
val pairs = withoutBrackets.split("], [")
val pairList: List<Pair<Double, Double>> = pairs.map {
val coords = it.removeSurrounding("[", "]").split(", ")
Pair(coords[0].toDouble(), coords[1].toDouble())
}
println(pairList)
答案3
得分: 0
这个答案可以是一个解决方案!
fun parseString(str: String): MutableList<Pair<Double, Double>> {
val pairs = mutableListOf<Pair<Double, Double>>()
val elements = str
.removeSurrounding("[[", "]]")
.replace(" ", "")
.split("],[")
elements.forEach { element ->
val (firstElement, secondElement) = element.split(",")
val pair = Pair(firstElement.toDouble(), secondElement.toDouble())
pairs.add(pair)
}
return pairs
}
英文:
This answer can be a solution!
fun parseString(str: String): MutableList<Pair<Double, Double>> {
val pairs = mutableListOf<Pair<Double, Double>>()
val elements = str
.removeSurrounding("[[", "]]")
.replace(" ", "")
.split("],[")
elements.forEach { element ->
val (firstElement, secondElement) = element.split(",")
val pair = Pair(firstElement.toDouble(), secondElement.toDouble())
pairs.add(pair)
}
return pairs
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论