如何在 Kotlin 中解析 JSON? [英] How to parse JSON in Kotlin?

查看:109
本文介绍了如何在 Kotlin 中解析 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务接收到一个非常深的 JSON 对象字符串,我必须将其解析为 JSON 对象,然后将其映射到类.

I'm receiving a quite deep JSON object string from a service which I must parse to a JSON object and then map it to classes.

如何在 Kotlin 中将 JSON 字符串转换为对象?

How can I transform a JSON string to object in Kotlin?

在映射到相应类之后,我使用了 Jackson 的 StdDeserializer.问题出现在对象具有也必须反序列化为类的属性时.我无法在另一个反序列化器中获得对象映射器,至少我不知道如何获得.

After that the mapping to the respective classes, I was using StdDeserializer from Jackson. The problem arises at the moment the object had properties that also had to be deserialized into classes. I was not able to get the object mapper, at least I didn't know how, inside another deserializer.

最好是,在本机上,我试图减少我需要的依赖项的数量,因此如果答案仅用于 JSON 操作和解析就足够了.

Preferably, natively, I'm trying to reduce the number of dependencies I need so if the answer is only for JSON manipulation and parsing it'd be enough.

推荐答案

你可以使用这个库 https://github.com/cbeust/klaxon

Klaxon 是一个在 Kotlin 中解析 JSON 的轻量级库.

Klaxon is a lightweight library to parse JSON in Kotlin.

这篇关于如何在 Kotlin 中解析 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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