1

I'm new on Kotlin and I'm trying parse a Json file to take your values. The first problem is that I don't know the keys and the second problem is that I don't know use Kotlin perfectely. Let me explain the case.

This is a Json format that I getting from web:

{
    "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"
        },
        ...
   }
}

Note that keys can change by time. So far, I can get the file and print it:

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)
}

and it's ok, the content is printed in one line correctely. So, now I want iterate on this keys like a map.iterator. How do it on Kotlin?

Augusto
  • 3,825
  • 9
  • 45
  • 93

1 Answers1

2

You can use various methods of json objects like jsonObject, values, entries etc. For example:

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")
        }
    }
}

will print

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"
Andrei Tanana
  • 7,932
  • 1
  • 27
  • 36