在Kotlin中将字符串转换为列表

huangapple go评论56阅读模式
英文:

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 = &quot;[[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]]&quot;

val withoutBrackets = jsonString.removeSurrounding(&quot;[&quot;, &quot;]&quot;)
val pairs = withoutBrackets.split(&quot;], [&quot;)
val pairList: List&lt;Pair&lt;Double, Double&gt;&gt; = pairs.map {
    val coords = it.removeSurrounding(&quot;[&quot;, &quot;]&quot;).split(&quot;, &quot;)
    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&lt;Pair&lt;Double, Double&gt;&gt; {
    val pairs = mutableListOf&lt;Pair&lt;Double, Double&gt;&gt;()

    val elements = str
        .removeSurrounding(&quot;[[&quot;, &quot;]]&quot;)
        .replace(&quot; &quot;, &quot;&quot;)
        .split(&quot;],[&quot;)

    elements.forEach { element -&gt;
        val (firstElement, secondElement) = element.split(&quot;,&quot;)
        val pair = Pair(firstElement.toDouble(), secondElement.toDouble())
        pairs.add(pair)
    }
    return pairs
}

huangapple
  • 本文由 发表于 2023年7月3日 20:38:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定