有沒有簡單的方法來轉換嵌套對象如何轉換爲JSON?我試圖創建一個JSON對象來匹配後端。我正在使用Retrofit進行網絡連接,它使用Gson將對象轉換爲JSON。Gson - 將嵌套對象序列化爲屬性
我沒有訪問網絡調用和轉換之間的任何代碼,所以我試圖找到一個乾淨的方式來修改如何通過GsonBuilder或註釋轉換對象。
// Automatically converted to JSON with passed in Gson.
Call<myObject> search(@Body foo myFoo);
public class foo {
String text = "boo";
bar b = new bar();
}
public class bar {
String other = "moo";
}
結果:
{ "text": "boo", "b" { "other": "moo" } }
所需的結果:
{ "text": "boo", "other": "moo" }
感謝您的幫助。 :)
您可以使用String other定義用於JSON轉換/從轉換中使用的虛擬類;字符串文本;並吸氣劑安裝者。 @Awestruck – pratikpawar