Home > other >  Issue when reading json data
Issue when reading json data

Time:01-05

This is my json

{
  "taxEngineCriteria": {
    "configJurisdiction": {
      "countryCode": "USA",
      "state": "IA"
    },
    "tenant": "Compliance",
    "legalEntities": ["\u0027\u0027"],
    "routingAttributes": [{
      "key": "business_model_name",
      "value": "car"
    }, {
      "key": "business_model_subtype_name",
      "value": "business_model_subtype_name"
    }],

    "configId": "1ae964cb-ab87-4b30-9a43-b0e85f8f1b21",
    "effectiveStartDate": {
    "day": 1,
    "month": 1,
    "year": 2019
   }
  }
}

This is the code I read data from json

data class TaxEngineCriteria(
        val configJurisdiction: ConfigJurisdiction,
        val tenant: String,
        val legalEntities: List<String>,
        val routingAttributes: List<RoutingAttribute>,
    )

    data class ConfigJurisdiction(
        val countryCode: String,
        val state: String
    )

    data class RoutingAttribute(
        val key: String,
        val value: String
    )

    val obj1 = gson.fromJson(jsonFileName, TaxEngineCriteria::class.java)

But the obj1 is null, how to fix this?

TaxEngineCriteria(seller=null, productType=null, configJurisdiction=null, tenant=null, legalEntities=null, routingAttributes=null, environment=null)

The thing is, when I using the same approach to read config data, it works, what's going on? is something wrong with the json format?

data class Config(
        val configId: String,
        val effectiveStartDate: Date,
    )

    data class Date(
        val day: Int,
        val month: Int,
        val year: Int
    )
            val obj = gson.fromJson(jsonFileName, tes1.Config::class.java)

CodePudding user response:

Everything looks good, except that you're missing an outer class. Create a class with a property taxEngineCriteria inside. For example:

data class JsonContent(val taxEngineCriteria: TaxEngineCriteria)

Now use this class to convert your json:

val content = gson.fromJson(jsonFileName, JsonContent::class.java)
  • Related