JSON / Kotlin - 处理JSON对象和数组

问题描述 投票:0回答:2

首先,我是JSON概念的新手,如果我的问题很愚蠢或非常简单,我很抱歉。

我想将Oxford Dictionary API用于我在Kotlin中编写的简单应用程序,这里是API作为JSON的响应:

{
    "metadata": {
        "provider": "Oxford University Press"
    },
    "results": [
        {
            "id": "hello",
            "language": "en",
            "lexicalEntries": [
                {
                    "entries": [
                        {
                            "etymologies": [
                                "early 19th century: variant of earlier hollo; related to holla"
                            ],
                            "homographNumber": "000",
                            "senses": [
                                {
                                    "definitions": [
                                        "used as a greeting or to begin a telephone conversation"
                                    ],
                                    "examples": [
                                        {
                                            "text": "hello there, Katie!"
                                        }
                                    ],
                                    "id": "m_en_gbus0460730.012",
                                    "short_definitions": [
                                        "used as greeting"
                                    ],
                                    "subsenses": [
                                        {
                                            "definitions": [
                                                "used to express surprise"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "hello, what's all this then?"
                                                }
                                            ],
                                            "id": "m_en_gbus0460730.017",
                                            "regions": [
                                                "British"
                                            ],
                                            "short_definitions": [
                                                "used to express surprise"
                                            ]
                                        },
                                        {
                                            "definitions": [
                                                "used as a cry to attract someone's attention"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "‘Hello below!’ he cried"
                                                }
                                            ],
                                            "id": "m_en_gbus0460730.018",
                                            "short_definitions": [
                                                "used attract attention"
                                            ]
                                        },
                                        {
                                            "definitions": [
                                                "used informally to express sarcasm or anger"
                                            ],
                                            "examples": [
                                                {
                                                    "text": "Hello! Did you even get what the play was about?"
                                                }
                                            ],
                                            "id": "m_en_gbus0460730.019",
                                            "short_definitions": [
                                                "used informally to express sarcasm or anger"
                                            ]
                                        }
                                    ]

现在,我想从这个JSON对象中只提取“定义”,但是你可以看到它嵌套在其他JSON数组中,到目前为止,我的代码如下所示:

                var resultJSON = JSONObject(result)


            var JSON_results = resultJSON.getJSONArray("results")
            var JSON_lexical = JSON_results.getJSONObject(0).getJSONArray("lexicalEntries")
            var JSON_entries = JSON_lexical.getJSONObject(0).getJSONArray("entries")
            var JSON_senses = JSON_entries.getJSONObject(0).getJSONArray("senses")
            var JSON_definitions = JSON_senses.getJSONObject(0).getJSONArray("definitions")


            Log.i("JSON", JSON_definitions.getString(0))

我知道需要更好的方法来做到这一点,但我找不到如何做到这一点。

json kotlin
2个回答
0
投票

尝试如下

val justDefinitions = mutableListOf<String>()
JSON_senses.forEach {
    val definitions = it.getJSONArray("definitions")
    for (i in 0 until definitions.length()) { {
        justDefinitions.add(it.getString(i))
    }
}

0
投票

Kotlin实际上可以更容易地将这种响应映射到称为“数据类”的东西。因此,您只需将JSON响应粘贴到Kotlin数据类生成器的在线JSON中,例如https://json2kotlin.com

它会像这样生成.kt文件:

data class Json4Kotlin_Base (

    val metadata : Metadata,
    val results : List<Results>
)

然后你可以简单地将响应JSON传递给Data类映射,如下所示:

val json = getJson() // your json value here
val topic = Gson().fromJson(json, Json4Kotlin_Base::class.java)

如果您在生成的模型中查找GSON注释,请在生成模型时选择该选项。

这是一个关于它的分步过程的视频教程。 https://www.youtube.com/watch?v=n46WbgNoEnE

© www.soinside.com 2019 - 2024. All rights reserved.