Bart
09/22/2021, 9:45 PMcall.receive
Example json:
{Key:[{Var:value, var:value},{Var:value, var:value}]}
Any idea how i can handle more that one in single route? Also did its possible to combine two keys in one route?Aleksei Tirman [JB]
09/23/2021, 9:08 AMvalue
is a string then receiving works with the following code:
fun main() {
embeddedServer(Netty, port = 8080) {
install(ContentNegotiation) {
json()
}
routing {
post("/json") {
val r = call.receive<Request>()
println(r)
}
}
}.start()
}
@Serializable
data class Request(val Key: List<Entry>)
@Serializable
data class Entry(@SerialName("Var") val var1: Value, @SerialName("var") val var2: Value)
typealias Value = String
Bart
09/23/2021, 4:32 PMAleksei Tirman [JB]
09/23/2021, 5:20 PMBart
09/23/2021, 5:32 PM{"Header": {
"OrderNo":"someInt",
"Created by":"someString",
"Company":"someString",
"Status":"someString",
"CreateDate":"someLong",
"EstimateDate":"someLong",
"EndDate":"someLong",
"Assigned to":"someString",
"Priority":"someBool",
"Reference":"someString"},
"Body1":{
"headerID":"someInt",
"Item":"someString",
"Quantity":"someInt",
"Position":"someString",
"Warehouse":"someString",
"Confirmation":"someBool"},
"Body2":{
"headerID":"someInt",
"Item":"someString",
"Quantity":"someInt",
"Position":"someString",
"Warehouse":"someString",
"Confirmation":"someBool"
}}
Aleksei Tirman [JB]
09/24/2021, 8:59 AMHeader
is the details of an order and each Body
element is the details of an item in that order. If so then I suggest making an array for a list of items:
{
"Header": {
"OrderNo": "someInt",
"Created by": "someString",
"Company": "someString",
"Status": "someString",
"CreateDate": "someLong",
"EstimateDate": "someLong",
"EndDate": "someLong",
"Assigned to": "someString",
"Priority": "someBool",
"Reference": "someString"
},
"Items": [
{
"headerID": "someInt",
"Item": "someString",
"Quantity": "someInt",
"Position": "someString",
"Warehouse": "someString",
"Confirmation": "someBool"
},
{
"headerID": "someInt",
"Item": "someString",
"Quantity": "someInt",
"Position": "someString",
"Warehouse": "someString",
"Confirmation": "someBool"
}
]
}
So to deserialize it you will need to have a class for the root JSON object, a class for a header, and a class for an item.
Does it make sense?