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

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

Convert a String into a List in Kotlin

问题

在Kotlin中,我从服务器收到这个字符串形式的地理坐标列表:

  1. [[45.02498, 7.55163], [45.02527, 7.55167], [45.02556, 7.55172],
  2. [45.02564, 7.55173], [45.02582, 7.55175], [45.02582, 7.55188],
  3. [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 循环可以访问所有元素,提取所需的数据。

  1. val jsonArray = JSONArray(receivedText)
  2. for (i in 0 until jsonArray.length()) {
  3. val pairArray = jsonArray.getJSONArray(i)
  4. val latitude = pairArray.getDouble(0)
  5. val longitude = pairArray.getDouble(1)
  6. Log.d("description","Pair $i - Latitude: $latitude, Longitude: $longitude")
  7. }
英文:

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.

  1. val jsonArray = JSONArray(receivedText)
  2. for (i in 0 until jsonArray.length()) {
  3. val pairArray = jsonArray.getJSONArray(i)
  4. val latitude = pairArray.getDouble(0)
  5. val longitude = pairArray.getDouble(1)
  6. Log.d("description","Pair $i - Latitude: $latitude, Longitude: $longitude")

答案2

得分: 0

尝试这个:

  1. 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]]"
  2. val withoutBrackets = jsonString.removeSurrounding("[", "]")
  3. val pairs = withoutBrackets.split("], [")
  4. val pairList: List<Pair<Double, Double>> = pairs.map {
  5. val coords = it.removeSurrounding("[", "]").split(", ")
  6. Pair(coords[0].toDouble(), coords[1].toDouble())
  7. }
  8. println(pairList)
英文:

Try this:

  1. 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;
  2. val withoutBrackets = jsonString.removeSurrounding(&quot;[&quot;, &quot;]&quot;)
  3. val pairs = withoutBrackets.split(&quot;], [&quot;)
  4. val pairList: List&lt;Pair&lt;Double, Double&gt;&gt; = pairs.map {
  5. val coords = it.removeSurrounding(&quot;[&quot;, &quot;]&quot;).split(&quot;, &quot;)
  6. Pair(coords[0].toDouble(), coords[1].toDouble())
  7. }
  8. println(pairList)

答案3

得分: 0

  1. 这个答案可以是一个解决方案!
  2. fun parseString(str: String): MutableList<Pair<Double, Double>> {
  3. val pairs = mutableListOf<Pair<Double, Double>>()
  4. val elements = str
  5. .removeSurrounding("[[", "]]")
  6. .replace(" ", "")
  7. .split("],[")
  8. elements.forEach { element ->
  9. val (firstElement, secondElement) = element.split(",")
  10. val pair = Pair(firstElement.toDouble(), secondElement.toDouble())
  11. pairs.add(pair)
  12. }
  13. return pairs
  14. }
英文:

This answer can be a solution!

  1. fun parseString(str: String): MutableList&lt;Pair&lt;Double, Double&gt;&gt; {
  2. val pairs = mutableListOf&lt;Pair&lt;Double, Double&gt;&gt;()
  3. val elements = str
  4. .removeSurrounding(&quot;[[&quot;, &quot;]]&quot;)
  5. .replace(&quot; &quot;, &quot;&quot;)
  6. .split(&quot;],[&quot;)
  7. elements.forEach { element -&gt;
  8. val (firstElement, secondElement) = element.split(&quot;,&quot;)
  9. val pair = Pair(firstElement.toDouble(), secondElement.toDouble())
  10. pairs.add(pair)
  11. }
  12. return pairs
  13. }

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:

确定