1

I have to send data in the following model:

{
"TransactionType": "testType",
"TransactionReference": "testSectionReference",
"TransactionDate": "2018-10-22T06:22:30.632Z",
"TransactionLines": [
{
  "ProductReference": "testProductReference",
  "ShipDate": "2018-10-22T06:22:30.632Z",
  "Quantity": 1,
  "AwardAmount": 2.0,
  "TotalAmount": 3.0
}
]
}

I do so by creating a transactionBody, which I send as a body in the request like this:

@POST("/myCustomUrlPath")
Call<Transaction> createTransaction(
        @Body TransactionBody transactionBody
        );

The TransactionBody has the following parameter:

transactionType - String

transactionReference - String

transactionDate - String

transactionLines - TransactionLines //(my custom model)

All seems good, until I test the request and see that the properties of my TransactionLines model are not sent like this:

"productReference":"testProductReference"

but instead they are sent with my java class path like this:

"model.transactionLines.productReference":"testProductReference"

This of course makes my server return an error, because it's expecting productReference and not model.transactionLines.productReference. How do I remove the path of my java class model before the variable name?

Edit:

I don't think my problem is anything close to what is suggested as a possible already asked question. They are asking for posting an array in json and I am having problem with posting the name of a variable in a custom object used in json post.

However, @Jeel Vankhede is right. Serializing the name of the variable removed the path of my java class and now the request is filled with correct data. Thank you!

G. Rann
  • 324
  • 1
  • 14

1 Answers1

3

As Jeel suggested in the comment, you should use the @SerializedName annotation in your model class. This is how your model should look:

@SerializedName("productReference")
private String productReference;

In your TransactionLines class. When this property is not specified, it'll just use the default name for serialization, which usually works out, but if for some reason you want a different one, that's where you specify it. I use it for example when I really dislike naming my Java variable like some_property, and API needs it to be called like that, I'll do something like:

@SerializedName("some_property")
private String someProperty;
Vucko
  • 7,371
  • 2
  • 27
  • 45