2017-10-21 41 views
1

Je suis en train de désérialiser un objet de typeComment sérialiser ArrayList de Pair utilisant gson

ArrayList<Pair<OuterData, ArrayList<InnerData>>>

OuterData et InnerData sont POJO en utilisant gson.

J'ai essayé si fort mais je ne suis pas capable de le faire. Je reçois

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap ne peut pas être jeté à v2015.oasis.pilani.bits.com.home.events.inner.InnerData

chaque fois que je tente de faire new Gson().fromJson(json, type);

où le type est obtenu à partir TypeToken à l'aide

new TypeToken<ArrayList<Pair<OuterData, ArrayList<InnerData>>>>(){}.getType() 

Je pense que l'utilisation de Type résoudra mon problème car initialement je ne l'utiliserais pas mais ce n'est pas le cas. Toute aide est appréciée.

Edit: Voici les OuterData et InnerData classes (En Kotlin)

data class InnerData(val name: String, 
        val category: String, 
        val categoryIcon: Int, 
        val description: String, 
        val rules: String, 
        val time: String, 
        val date: String, 
        val venue: String, 
        val notifyState: Boolean, 
        val notifyTime: Int, 
        val favouriteState: Boolean) 

data class OuterData(val heading: String, val color: Int) 

J'utilise gson seulement pour sérialiser les données. Donc, la désérialisation utilise le même json sérialisé en utilisant gson.

Edit2: sérialisé JSON données: Il était la sérialisation de sortie en utilisant gson

[ 
{ 
    "first": { 
     "color": -65281, 
     "heading": "October 01" 
    }, 
    "second": [ 
     { 
      "category": "", 
      "categoryIcon": 17301533, 
      "date": "", 
      "description": "sfd", 
      "favouriteState": false, 
      "name": "sdfds", 
      "notifyState": false, 
      "notifyTime": 0, 
      "rules": "", 
      "time": "", 
      "venue": "" 
     } 
    ] 
}, 
{ 
    "first": { 
     "color": -65281, 
     "heading": "November 01" 
    }, 
    "second": [ 
     { 
      "category": "", 
      "categoryIcon": 17301533, 
      "date": "djfkd", 
      "description": "klddjflk", 
      "favouriteState": false, 
      "name": "jkl", 
      "notifyState": false, 
      "notifyTime": 0, 
      "rules": "", 
      "time": "sdkjfk", 
      "venue": "ldkfjf" 
     } 
    ] 
}, 
{ 
    "first": { 
     "color": -16175867, 
     "heading": "October 31" 
    }, 
    "second": [ 
     { 
      "category": "Event Category", 
      "categoryIcon": 17301533, 
      "date": "31-10-2017", 
      "description": "Event Description", 
      "favouriteState": false, 
      "name": "Event name", 
      "notifyState": false, 
      "notifyTime": 0, 
      "rules": "Events Rules", 
      "time": "13:55", 
      "venue": "Event Venue" 
     }, 
     { 
      "category": "", 
      "categoryIcon": 17301533, 
      "date": "", 
      "description": "", 
      "favouriteState": false, 
      "name": "dsf", 
      "notifyState": false, 
      "notifyTime": 0, 
      "rules": "", 
      "time": "", 
      "venue": "" 
     }, 
     { 
      "category": "", 
      "categoryIcon": 17301533, 
      "date": "", 
      "description": "", 
      "favouriteState": false, 
      "name": "", 
      "notifyState": false, 
      "notifyTime": 0, 
      "rules": "", 
      "time": "", 
      "venue": "" 
     }, 
     { 
      "category": "", 
      "categoryIcon": 17301533, 
      "date": "", 
      "description": "", 
      "favouriteState": false, 
      "name": "", 
      "notifyState": false, 
      "notifyTime": 0, 
      "rules": "", 
      "time": "", 
      "venue": "" 
     }, 
     { 
      "category": "", 
      "categoryIcon": 17301533, 
      "date": "", 
      "description": "", 
      "favouriteState": false, 
      "name": "", 
      "notifyState": false, 
      "notifyTime": 0, 
      "rules": "", 
      "time": "", 
      "venue": "" 
     }, 
     { 
      "category": "", 
      "categoryIcon": 17301533, 
      "date": "", 
      "description": "", 
      "favouriteState": false, 
      "name": "", 
      "notifyState": false, 
      "notifyTime": 0, 
      "rules": "", 
      "time": "", 
      "venue": "" 
     }, 
     { 
      "category": "", 
      "categoryIcon": 17301533, 
      "date": "", 
      "description": "", 
      "favouriteState": false, 
      "name": "", 
      "notifyState": false, 
      "notifyTime": 0, 
      "rules": "", 
      "time": "", 
      "venue": "" 
     }, 
     { 
      "category": "", 
      "categoryIcon": 17301533, 
      "date": "", 
      "description": "", 
      "favouriteState": false, 
      "name": "", 
      "notifyState": false, 
      "notifyTime": 0, 
      "rules": "", 
      "time": "", 
      "venue": "" 
     }, 
     { 
      "category": "", 
      "categoryIcon": 17301533, 
      "date": "", 
      "description": "", 
      "favouriteState": false, 
      "name": "", 
      "notifyState": false, 
      "notifyTime": 0, 
      "rules": "", 
      "time": "", 
      "venue": "" 
     } 
    ] 
}] 
+0

Pouvez-vous poster des exemples JSON et les classes que vous utilisez? –

+0

J'ai modifié la question. Jetez un coup d'oeil s'il vous plait. –

+0

Pouvez-vous s'il vous plaît poster quelques JSON aussi? – Ellisan

Répondre

0

Je l'ai. Le problème était que j'utilisais la classe Pair de kotlin. Dès que j'ai défini ma propre classe de Paire, tout a fonctionné correctement.

0

J'ai essayé votre code et cela fonctionne:

import com.google.common.base.Charsets; 
import com.google.common.io.Resources; 
import com.google.gson.Gson; 
import com.google.gson.reflect.TypeToken; 
import java.lang.reflect.Type; 
import java.util.List; 

public class App { 
    public static void main(String[] args) throws Exception { 
     Gson gson = new Gson(); 
     final Type type = new TypeToken<List<Pair<OuterData, List<InnerData>>>>() {}.getType(); 
     final List<Pair<OuterData, List<InnerData>>> o = new Gson().fromJson(Resources.toString(Resources.getResource("foo.json"), Charsets.UTF_8), type); 
     System.out.println(gson.toJson(o)); 
     System.out.println(o.get(0).getFirst().getColor()); 
    } 

    static class Pair<F, S> { 
     F first; 
     S second; 

     public F getFirst() { 
      return first; 
     } 

     public void setFirst(F first) { 
      this.first = first; 
     } 

     public S getSecond() { 
      return second; 
     } 

     public void setSecond(S second) { 
      this.second = second; 
     } 
    } 

    static class OuterData { 
     String heading; 
     Integer color; 

     public String getHeading() { 
      return heading; 
     } 

     public void setHeading(String heading) { 
      this.heading = heading; 
     } 

     public Integer getColor() { 
      return color; 
     } 

     public void setColor(Integer color) { 
      this.color = color; 
     } 
    } 

    static class InnerData { 
     String name; 
     String category; 
     Integer categoryIcon; 
     String description; 
     String rules; 
     String time; 
     String date; 
     String venue; 
     Boolean notifyState; 
     Integer notifyTime; 
     Boolean favouriteState; 

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 

     public String getCategory() { 
      return category; 
     } 

     public void setCategory(String category) { 
      this.category = category; 
     } 

     public Integer getCategoryIcon() { 
      return categoryIcon; 
     } 

     public void setCategoryIcon(Integer categoryIcon) { 
      this.categoryIcon = categoryIcon; 
     } 

     public String getDescription() { 
      return description; 
     } 

     public void setDescription(String description) { 
      this.description = description; 
     } 

     public String getRules() { 
      return rules; 
     } 

     public void setRules(String rules) { 
      this.rules = rules; 
     } 

     public String getTime() { 
      return time; 
     } 

     public void setTime(String time) { 
      this.time = time; 
     } 

     public String getDate() { 
      return date; 
     } 

     public void setDate(String date) { 
      this.date = date; 
     } 

     public String getVenue() { 
      return venue; 
     } 

     public void setVenue(String venue) { 
      this.venue = venue; 
     } 

     public Boolean getNotifyState() { 
      return notifyState; 
     } 

     public void setNotifyState(Boolean notifyState) { 
      this.notifyState = notifyState; 
     } 

     public Integer getNotifyTime() { 
      return notifyTime; 
     } 

     public void setNotifyTime(Integer notifyTime) { 
      this.notifyTime = notifyTime; 
     } 

     public Boolean getFavouriteState() { 
      return favouriteState; 
     } 

     public void setFavouriteState(Boolean favouriteState) { 
      this.favouriteState = favouriteState; 
     } 
    } 
} 

La sortie est:

[{"first":{"heading":"October 01","color":-65281},"second":[{"name":"sdfds","category":"","categoryIcon":17301533,"description":"sfd","rules":"","time":"","date":"","venue":"","notifyState":false,"notifyTime":0,"favouriteState":false}]},{"first":{"heading":"November 01","color":-65281},"second":[{"name":"jkl","category":"","categoryIcon":17301533,"description":"klddjflk","rules":"","time":"sdkjfk","date":"djfkd","venue":"ldkfjf","notifyState":false,"notifyTime":0,"favouriteState":false}]},{"first":{"heading":"October 31","color":-16175867},"second":[{"name":"Event name","category":"Event Category","categoryIcon":17301533,"description":"Event Description","rules":"Events Rules","time":"13:55","date":"31-10-2017","venue":"Event Venue","notifyState":false,"notifyTime":0,"favouriteState":false},{"name":"dsf","category":"","categoryIcon":17301533,"description":"","rules":"","time":"","date":"","venue":"","notifyState":false,"notifyTime":0,"favouriteState":false},{"name":"","category":"","categoryIcon":17301533,"description":"","rules":"","time":"","date":"","venue":"","notifyState":false,"notifyTime":0,"favouriteState":false},{"name":"","category":"","categoryIcon":17301533,"description":"","rules":"","time":"","date":"","venue":"","notifyState":false,"notifyTime":0,"favouriteState":false},{"name":"","category":"","categoryIcon":17301533,"description":"","rules":"","time":"","date":"","venue":"","notifyState":false,"notifyTime":0,"favouriteState":false},{"name":"","category":"","categoryIcon":17301533,"description":"","rules":"","time":"","date":"","venue":"","notifyState":false,"notifyTime":0,"favouriteState":false},{"name":"","category":"","categoryIcon":17301533,"description":"","rules":"","time":"","date":"","venue":"","notifyState":false,"notifyTime":0,"favouriteState":false},{"name":"","category":"","categoryIcon":17301533,"description":"","rules":"","time":"","date":"","venue":"","notifyState":false,"notifyTime":0,"favouriteState":false},{"name":"","category":"","categoryIcon":17301533,"description":"","rules":"","time":"","date":"","venue":"","notifyState":false,"notifyTime":0,"favouriteState":false}]}] 
-65281