0

我是 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 上如何实现?

4

1 回答 1

1

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

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"
于 2019-09-30T19:36:04.677 回答