如何使用 kotlinx 在 Kotlin 上迭代具有未知键的 JsonElement?

How to iterate a JsonElement with unknown keys on Kotlin using kotlinx?

我是 Kotlin 的新手,我正在尝试解析 Json 文件以获取您的值。第一个问题是我不知道密钥,第二个问题是我不知道如何完美地使用 Kotlin。我来解释一下。

这是我从网上获得的 Json 格式:

{
    "Meta Data": {
        "1. Information": "Intraday (5min) open, high, low, close prices and volume",
        "2. Symbol": "MRFG3.SA",
        "3. Last Refreshed": "2019-09-30 13:05:00",
        "4. Interval": "5min",
        "5. Output Size": "Compact",
        "6. Time Zone": "US/Eastern"
    },
    "Time Series (5min)": {
        "2019-09-30 13:05:00": {
            "1. open": "10.9400",
            "2. high": "10.9500",
            "3. low": "10.9300",
            "4. close": "10.9300",
            "5. volume": "6400"
        },
        "2019-09-30 13:00:00": {
            "1. open": "10.9600",
            "2. high": "10.9600",
            "3. low": "10.9200",
            "4. close": "10.9500",
            "5. volume": "33657"
        },
        "2019-09-30 12:55:00": {
            "1. open": "10.9705",
            "2. high": "10.9906",
            "3. low": "10.9605",
            "4. close": "10.9605",
            "5. volume": "36767"
        },
        ...
   }
}

请注意,密钥会随时间变化。到目前为止,我可以获取文件并打印它:

import kotlinx.serialization.json.Json
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse

fun getContent() {
    val client = HttpClient.newBuilder().build()
    val request = HttpRequest.newBuilder()
            .uri(URI.create("https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&interval=1min&symbol=MRFG3.SA&apikey=xxx"))
            .build()
    val response = client.send(request,HttpResponse.BodyHandlers.ofString())
    val wholeJson = Json.nonstrict.parseJson(response.body())
    println(wholeJson)
}

没关系,内容正确打印在一行中。所以,现在我想像 map.iterator 一样迭代这个键。如何在 Kotlin 上实现?

您可以使用 json 对象的各种方法,例如 jsonObjectvaluesentries 等。例如:

import kotlinx.serialization.json.Json

fun main() {
    val json = """
{
    "Meta Data": {
        "1. Information": "Intraday (5min) open, high, low, close prices and volume",
        "2. Symbol": "MRFG3.SA",
        "3. Last Refreshed": "2019-09-30 13:05:00",
        "4. Interval": "5min",
        "5. Output Size": "Compact",
        "6. Time Zone": "US/Eastern"
    },
    "Time Series (5min)": {
        "2019-09-30 13:05:00": {
            "1. open": "10.9400",
            "2. high": "10.9500",
            "3. low": "10.9300",
            "4. close": "10.9300",
            "5. volume": "6400"
        },
        "2019-09-30 13:00:00": {
            "1. open": "10.9600",
            "2. high": "10.9600",
            "3. low": "10.9200",
            "4. close": "10.9500",
            "5. volume": "33657"
        },
        "2019-09-30 12:55:00": {
            "1. open": "10.9705",
            "2. high": "10.9906",
            "3. low": "10.9605",
            "4. close": "10.9605",
            "5. volume": "36767"
        }
   }
}
    """.trimIndent()
    val wholeJson = Json.nonstrict.parseJson(json).jsonObject
    val timeSeries = wholeJson.values.last().jsonObject
    for ((time, series) in timeSeries) {
        println(time)
        for ((key, value) in series.jsonObject) {
            println("$key $value")
        }
    }
}

将打印

2019-09-30 13:05:00
1. open "10.9400"
2. high "10.9500"
3. low "10.9300"
4. close "10.9300"
5. volume "6400"
2019-09-30 13:00:00
1. open "10.9600"
2. high "10.9600"
3. low "10.9200"
4. close "10.9500"
5. volume "33657"
2019-09-30 12:55:00
1. open "10.9705"
2. high "10.9906"
3. low "10.9605"
4. close "10.9605"
5. volume "36767"