2016-04-06 184 views
0

我有一個服務返回的字符串,在這個JSON格式:轉換一個JSON字符串轉換成Java對象

String message = { 
    "Tickets": 
      [{ 
        "Type": "type1", 
        "Author": "author1", 
        "Rows": 
          [ 
           { 
            "Price": "100.0", 
            "Date": "24/06/2016", 
            "Amount": "10" 
           }, 
           { 
            "Type": "Comment", 
            "Value": "some comment goes here" 
           } 
          ], 
        "ID": "165" 
       }], 
    "Desk": "desk1", 
    "User": "user1" 
} 

我需要解析它並轉換成Java對象。 我試圖創建這樣一個DOM:

public class TicketWrapper{ 
    private Ticket ticket; 
    private String desk; 
    private String user; 
} 

public class Ticket { 
    private String type; 
    private String author; 
    private List<Row> rows; 
    private String id; 
} 

public class Row1{ 
    private float price; 
    private Date date; 
    private int amount; 
} 

public class Row2{ 
    private String type; 
    private float value; 
} 

然後我嘗試用谷歌GSON解析它,這樣說:

TicketWrapper ticket = gson.fromJson(message, TicketWrapper.class) 

,但如果我打印System.out.println(gson.toJson(ticket)),它打印: {」桌面「:0,」用戶「:0}

我不知道如何將Json解析爲Java對象,以及如何告訴他一行到」行「可以是Row1類型或Row2類型。

+3

案件很重要。 – Savior

+2

在JSON中,該屬性被命名爲「Tickets」,帶有's',正如名稱所示,它是一系列票據,而不僅僅是一張票。還不清楚Row是什麼,以及Row1和Row2是相關的。 –

+0

我建議你閱讀一些關於使用Gson.fromJson的教程 – Pooya

回答

1

我認爲有幾個問題,如小寫和日期格式的屬性名稱和混合類型的行。我只是改變這樣,爲我工作:

import com.google.gson.Gson; 
import com.google.gson.GsonBuilder; 
import com.google.gson.annotations.SerializedName; 
import org.junit.Test; 
import java.util.Date; 
import java.util.List; 

public class CheckTest { 

    @Test 
    public void thisTest() { 
     Gson gson = new GsonBuilder() 
       .setDateFormat("dd-MM-yyyy") 
       .setPrettyPrinting() 
       .create(); 
     String message = "{\"Tickets\":" + 
       "[{\"Type\":\"type1\"," + 
       "\"Author\":\"author1\"," + 
       "\"Rows\":[{\"Price\":\"100.0\"," + 
       "\"Date\":\"24-06-2016\"," + 
       "\"Amount\":\"10\"}," + 
       "{\"Type\":\"Comment\"," + 
       "\"Value\":\"some comment goes here\"}]," + 
       "\"ID\":\"165\"}]," + 
       "\"Desk\":\"desk1\"," + 
       "\"User\":\"user1\"}"; 
     TicketWrapper ticket = gson.fromJson(message, TicketWrapper.class); 
     System.out.println(ticket.toString()); 
    } 

    public class TicketWrapper { 
     @SerializedName("Tickets") 
     private List<Ticket> tickets; 
     @SerializedName("Desk") 
     private String desk; 
     @SerializedName("User") 
     private String user; 
     public TicketWrapper() { 
     } 
    } 

    public class Ticket { 
     @SerializedName("Type") 
     private String type; 
     @SerializedName("Author") 
     private String author; 
     @SerializedName("Rows") 
     private List<Row> rows; 
     @SerializedName("ID") 
     private String id; 

     public Ticket() { 
     } 
    } 

    public class Row { 
     @SerializedName("Type") 
     private String type; 
     @SerializedName("Value") 
     private String value; 
     @SerializedName("Price") 
     private float price; 
     @SerializedName("Date") 
     private Date date; 
     @SerializedName("Amount") 
     private int amount; 

     public Row() { 
     } 
    } 
} 
+0

謝謝,這就是我一直在尋找的! 只有一件事,有一種方法可以告訴gson: 如果行的類型爲Row1,則不填充Row2的字段,否則,如果是Row2,則不填充Row1的字段。 我試着做一些多態性,比如'Row1 extends Row'和'Row2 extends Row',並使用: 'private List <?擴展行>行' ,但它只填充基類的字段的行。 謝謝! – luca89pe

1

正如其他人在評論中已經提到的那樣,您需要確保映射直接反映文件名。它需要是'用戶'和'桌面'而不是'用戶'和'桌子'。另外,您還有一張門票列表,它將映射到列表門票。