0

First of all, I'm very new to JSON concept so sorry if my question is silly or very simple to answer.

I want to use Oxford Dictionary API for simple app which I'm writing in Kotlin, here is the response from API as 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"
                                            ]
                                        }
                                    ]

And now, I would like to extract only "definitions" from this JSON object but as you can see it is nested within other JSON arrays, my code so far looks like this:

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

I know that there needs to be a better way of doing this but I can't find how.

1
  • 1
    Don't you want use 3th party libraries like Gson or in case of Kotlin Kotson? Commented Jul 13, 2018 at 13:39

2 Answers 2

1

Kotlin actually makes it easier to map such responses with something called "data classes". So you can simply paste the JSON response in an online JSON to Kotlin Data Class Generator e.g. https://json2kotlin.com

It churns out .kt files like this:

data class Json4Kotlin_Base (

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

and thn you can simply pass on the response JSON to the Data class mapping like this:

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

In case you're looking for GSON annotations in the generated models, chose the option when you generate those.

Here's a video tutorial for step by step process about it. https://www.youtube.com/watch?v=n46WbgNoEnE

Sign up to request clarification or add additional context in comments.

Comments

0

Try as follow

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.