xref: /aosp_15_r20/external/kotlinx.serialization/guide/example/example-json-16.kt (revision 57b5a4a64c534cf7f27ac9427ceab07f3d8ed3d8)
1 // This file was automatically generated from json.md by Knit tool. Do not edit.
2 package example.exampleJson16
3 
4 import kotlinx.serialization.*
5 import kotlinx.serialization.json.*
6 
mainnull7 fun main() {
8     val element = Json.parseToJsonElement("""
9         {
10             "name": "kotlinx.serialization",
11             "forks": [{"votes": 42}, {"votes": 9000}, {}]
12         }
13     """)
14     val sum = element
15         .jsonObject["forks"]!!
16         .jsonArray.sumOf { it.jsonObject["votes"]?.jsonPrimitive?.int ?: 0 }
17     println(sum)
18 }
19