1

I am currently working on an app and I would like to have a JSON extraction to a class but it's a little more difficult that just matching field. I am using Moshi for the JSON management.

Below is the JSON I have :

{
  "common" :{
    "source_url": "xxxx",
    "target_url": "yyy
  },
  "specific":{
    "A":{
      "setting": "ccc",
    },
    "B":{
      "setting": "ddd",
    }
  }
}

The goal for me is to get an object based on a data class which look like this:

data class config(
    val sourceUrl: String,
    val targetUrl: String,
    val setting: String
)

What I have started is to first create the different data class to allow me to manipulate the data before getting the above class object.

--GeneralConfig.kt

data class GeneralConfig(
    val common: CommonConfig,
    val specific: Specific
)

-- CommonConfig.kt

data class CommonConfig(
    @Json(val name = "source_url") sourceUrl: String,
    @Json(val name = "target_url") val targetUrl: String
)

-- SpecificConfig.kt

data class SpecificConfig(
    setting: String,
)

I have 2 main questions:

  • How can I access the "specific" "A/B" data and get an object class SpecificConfig. Can I in my generalConfig data class directly extract one field from specific and select A or B by passing an argument

  • Is it possible to avoid creating multiple data class to get my final class object config

The purpose is to have a json containing a common data and specific data. I need to build a final data class which a merge from the common data and one of the specific data.

Any idea ? Thanks

1
  • Your class structure needs to match with your JSON structure. Your JSON "specific" object doesn't contain a single field named "setting". It contains two fields named A and B. And both of these fields are objects with a field named setting. So your Kotlin class structure needs to match that. Commented Oct 5, 2018 at 6:37

2 Answers 2

1

Try this 1. Modify your

data class SpecificConfig(
    setting: String,
)

To

data class SpecificConfig(
@Json(val name = "A") val a : JsonObject,
    @Json(val name = "B") val b: JsonObject
)

Second: Create another 2 classes i.e

data class AConfig(
    setting: String,
)

data class BConfig(
    setting: String,
)

Third: Modify

data class config(
    val sourceUrl: String,
    val targetUrl: String,
    val setting: String
)

To

data class config(
    val sourceUrl: String,
    val targetUrl: String,
    val setting: String,
    val a: JsonObject,
    val b: JsonObject
)
Sign up to request clarification or add additional context in comments.

Comments

1

I would prefer to create classes for every request and to not parse them manually

data class GeneralConfig(
        @SerializedName("common") val common: CommonConfig,
        @SerializedName("specific") val specific: Specific
                        )

data class CommonConfig(
        @SerializedName("source_url") val sourceUrl: String,
        @SerializedName("target_url") val targetUrl: String
                 )

data class Specific(
        @SerializedName("A") val a: SpecificSettings,
        @SerializedName("B") val b: SpecificSettings
                 )

data class SpecificSettings(
        @SerializedName("setting") val setting: String
                 )

1 Comment

ok thanks. I was trying to make a smarter code to avoid having a class for Specific by being able to pass A or B as an argument to get the "setting" field inside. A and B are used as example but you can have C, D,.... the goal is to have a json which can support more setting and avoid updating the code each time. I just had to pass the params A or B

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.