Parse same key but with different values to a data class in kotlin

Viewed 197

I am working on an API implementation with a third-party service, but the response I am getting from that API is not consistent, it has the same keys but different values of that key and I am not able to parse it, I tried it with Any, but can not get access to values as from the key, So If someone has achieved this or can suggest then it will be a great help, Thanks in advance. This is my JSON object response for example

 "data": [
              {
                "values": [
                  0.0,
                  0.2
                ],
                "type": "A"
              },
              {
                "values": [
                  [
                    [
                      -0.2,
                      5.4
                    ],
                    [
                      -0.2,
                      5.5
                    ],
                    [
                      0.3,
                      56.4
                    ],
                    [
                      0.5,
                      6.4
                    ]
                  ]
                ],
                "type": "P",
                "other": {
                  "type": "n",
                  "properties": {
                    "name": "u"
                  }
                }
              }
            ],

The above is how I am getting a response, But I am not able to find a proper way to parse it in a data class.

1 Answers

Use Jackson framework.

val objectMapper = ObjectMapper()
val treeNode = objectMapper.readTree("your_json_in_string")

treeNode is an JsonNode that contains your json tree.
See: jackson framework

Related