如何将JSON字符串映射到Kotlin Map [英] How to map a JSON string to Kotlin Map

查看:1523
本文介绍了如何将JSON字符串映射到Kotlin Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Kotlin程序,该程序可以访问Mongo数据库并生成如下所示的JSON字符串;

I have a simple Kotlin program that access a Mongo database and produce a JSON string as below;

"{
     "_id" : { "$oid" : "593440eb7fa580d99d1abe85"} , 
     "name" : "Firstname Secondname" ,
     "reg_number" : "ATC/DCM/1016/230" ,
     "oral" : 11 ,
     "oral_percent" : 73 , 
     "cat_1" : 57 , 
     "cat_2" : 60 , 
     "cat_average" : 59 , 
     "assignment" : 90
}"

如何在Kotlin Map/MutableMap中对此进行映射? Kotlin中是否有API可以读取JSON并将其映射到Map/MutableMap?

How do I map this in Kotlin Map/MutableMap? Is there an API in Kotlin to read JSON and map it to Map/MutableMap?

推荐答案

这可以通过 Klaxon .这样,您可以轻松地将 Json 数据读取为JsonObject,实际上是MutableMap.

This can be done with Klaxon. With this you can easily read the Json data as JsonObject which is actually a MutableMap.

val json: JsonObject = Parser().parse(jsonData) as JsonObject

这篇关于如何将JSON字符串映射到Kotlin Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆