如何使用kotlinx在Kotlin上使用未知密钥迭代JsonElement?

时间:2019-09-30 18:35:11

标签: json kotlin kotlinx.serialization

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

1 个答案:

答案 0 :(得分:0)

您可以使用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"