我必須以以下模型發(fā)送數(shù)據(jù):{"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}]}transactionBody為此,我創(chuàng)建了一個(gè),在請(qǐng)求中將其作為正文發(fā)送,如下所示:@POST("/myCustomUrlPath")Call<Transaction> createTransaction( @Body TransactionBody transactionBody );將TransactionBody具有以下參數(shù):交易類型 - String交易參考 - String交易日期 - String交易線 - TransactionLines //(my custom model)一切似乎都很好,直到我測(cè)試請(qǐng)求并看到我的TransactionLines模型的屬性不是這樣發(fā)送的:"productReference":"testProductReference"但相反,它們與我的 java 類路徑一起發(fā)送,如下所示:"model.transactionLines.productReference":"testProductReference"這當(dāng)然會(huì)使我的服務(wù)器返回錯(cuò)誤,因?yàn)樗谄诖齪roductReference而不是model.transactionLines.productReference. 如何在變量名之前刪除我的java類模型的路徑?編輯:我不認(rèn)為我的問(wèn)題與建議的可能已經(jīng)提出的問(wèn)題相近。他們要求在 json 中發(fā)布一個(gè)數(shù)組,而我在發(fā)布 json post 中使用的自定義對(duì)象中的變量名稱時(shí)遇到了問(wèn)題。但是,@Jeel Vankhede 是對(duì)的。序列化變量的名稱刪除了我的 java 類的路徑,現(xiàn)在請(qǐng)求填充了正確的數(shù)據(jù)。謝謝!
1 回答

猛跑小豬
TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
正如 Jeel 在評(píng)論中建議的那樣,您應(yīng)該@SerializedName在模型類中使用注釋。這是您的模型的外觀:
@SerializedName("productReference")
private String productReference;
在你的TransactionLines課上。當(dāng)未指定此屬性時(shí),它只會(huì)使用序列化的默認(rèn)名稱,這通常是可行的,但如果出于某種原因您想要不同的名稱,則可以在此處指定它。例如,當(dāng)我真的不喜歡將我的 Java 變量命名為some_property,而 API 需要像這樣調(diào)用它時(shí),我會(huì)使用它,我將執(zhí)行以下操作:
@SerializedName("some_property")
private String someProperty;
添加回答
舉報(bào)
0/150
提交
取消