2016-04-13 105 views
-3

JSON字符串:如何用Gson解析不同對象的Json數組?

[ 
     //Object 1 

     { 
      TypeName:"CheckSpecificDday", 
      SpecificDay:"20160413", 
      Lunar:1 
     }, 

     { 
      TypeName:"CheckSpecificDday", 
      SpecificDay:"20160414", 
      Lunar:1 
     }, 

     //Object 2 

     { 
      TypeName:"CheckEveryDayDday", 
      StartDate:"20160413", 
      EndDate:"20260417", 
      Interval:1, 
      StartOption:"D", 
      HolidayCondition:1 
     }, 

     //Object 3 

     { 
      TypeName:"CheckEveryDdayOfWeek", 
      StartDate:"20160413", 
      EndDate:"", 
      Interval:1, 
      SpecificDayOfWeek:"3", 
      HolidayCondition:1 
     }, 

     //Object 4 

     { 
      TypeName:"CheckEveryMonthSpecificDday", 
      StartDate:"20160413", 
      EndDate:"", 
      Interval:1, 
      SpecificDD:"13,14", 
      HolidayCondition:1 
     }, 

     //Object 5 

     { 
      TypeName:"CheckEveryYearWeek", 
      StartDate:"20160413", 
      EndDate:"", 
      Interval:1, 
      SpecificMMnthWeek:"0433", 
      HolidayCondition:1 
     } 

    ] 

我有一個JSON數組像上面。我想要的是用Gson解析它到不同的對象類型(正如我評論的那樣,使它更清晰),但我不知道如何做到這一點。請幫幫我。先謝謝你!

+0

你能發佈link API嗎?我將創建一個示例項目 –

+0

Gson不會爲您自動執行此操作。您必須編寫一些代碼來查看TypeName並確定其數據應填充到哪個Java對象。 –

+0

我知道,但我不知道如何。你能舉個例子嗎? – Kakashi

回答

9

我覺得在SO上有很多類似的問題。 One,解析這個Two

的方法之一是使用簡單的

Object[] result = new Gson().fromJson(json, Object[].class); 

但是,這會給你的LinkedTreeMap<Integer, LinkedTreeMap<String, String>>或類似這樣的東西的對象。你可以使用它,但它很難,而且你的整數也會出現問題,比如雙打。

另一種方法是創建自定義的接口或抽象類TypeName場,如果你需要它:

private interface CheckInterface{} 

,並與對象類型的每一個POJO類實現它,你必須:

private static class CheckEveryDayBase implements CheckInterface{ 
    private String StartDate; 
    private String EndDate; 
    private int Interval; 
    private int HolidayCondition; 
} 

private static class CheckSpecificDday implements CheckInterface{ 
    private String SpecificDay; 
    private int Lunar; 
} 

private static class CheckEveryDayDday extends CheckEveryDayBase{ 
    private String StartOption; 
} 

private static class CheckEveryDdayOfWeek extends CheckEveryDayBase{ 
    private String SpecificDayOfWeek; 
} 

private static class CheckEveryMonthSpecificDday extends CheckEveryDayBase{ 
    private String SpecificDD; 
} 

private static class CheckEveryYearWeek extends CheckEveryDayBase{ 
    private String SpecificMMnthWeek; 
} 

然後爲您的CheckInterface創建自定義解串器:

public static class CheckInterfaceDeserializer implements JsonDeserializer<CheckInterface>{ 

    @Override 
    public CheckInterface deserialize(JsonElement json, Type typeOfT, 
         JsonDeserializationContext context) throws JsonParseException { 
     JsonObject jObject = (JsonObject) json; 
     JsonElement typeObj = jObject.get("TypeName"); 

     if(typeObj!= null){ 
      String typeVal = typeObj.getAsString(); 

      switch (typeVal){ 
       case "CheckSpecificDday": 
        return context.deserialize(json, CheckSpecificDday.class); 
       case "CheckEveryDayDday": 
        return context.deserialize(json, CheckEveryDayDday.class); 
       case "CheckEveryDdayOfWeek": 
        return context.deserialize(json, CheckEveryDdayOfWeek.class); 
       case "CheckEveryMonthSpecificDday": 
        return context.deserialize(json, CheckEveryMonthSpecificDday.class); 
       case "CheckEveryYearWeek": 
        return context.deserialize(json, CheckEveryYearWeek.class); 
      } 
     } 

     return null; 
    } 
} 

以下是您可以如何使用的:

GsonBuilder builder = new GsonBuilder(); 

// Register custom deserializer for CheckInterface.class 
builder.registerTypeAdapter(CheckInterface.class, new CheckInterfaceDeserializer()); 
Gson gson = builder.create(); 

CheckInterface[] result2 = gson.fromJson(json, CheckInterface[].class);