PedroAGSantos
PedroAGSantos

Reputation: 2346

Deserializing json Using Gson not working

Class contains all the songs

public class Songs{
    private List levels;

    public List getLevels() {
        return levels;
    }

    public void setLevels(List levels) {
        this.levels = levels;
    }
}

each song object

public class Levels{
    private Number id;
    private String name;
    private List sequence;

    public Number getId(){
        return this.id;
    }
    public void setId(Number id){
        this.id = id;
    }
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name;
    }
    public List getSequence() {
        return sequence;
    }
    public void setSequence(List sequence) {
        this.sequence = sequence;
    }
}

JSON

{
    "levels": [
        {
            "id": 1,
            "name": "Sequence",
            "sequence": [
                17,
                1,
                2
            ]
        },
        {
            "id": 2,
            "name": "Sequence",
            "sequence": [
                17,
                0,
                1,
                2,
                4,
                4,
                5,
                6
            ]
        }
    ]
}

Java code

This works if I debug I can see the objects but the problem is getting sequence the array of int. Can Anyone help me ??? I can paste the stack trace if I do list2.getLevels().get(0).getSequence();

String json = new String(b);
Gson gson = new Gson();
Songs list2 = (Songs) gson.fromJson(json, Songs.class);

//I CAN READ ONE LEVEL LIKE THIS 
LinkedTreeMap<String,Levels> l = (LinkedTreeMap)songs.getLevels().get(0);


//HOW CAN I GET SEQUENCE ARRAY???

Upvotes: 0

Views: 791

Answers (2)

Serj Moya
Serj Moya

Reputation: 149

I'm going to paste a sample that I created but stored in shared preferences.

SharedPreferences settings = getSharedPreferences(String name, MODE_MULTI_PROCESS);
SharedPreferences.Editor editor = settings.edit();
Gson gson = new Gson();
String json = gson.toJson(ArrayList<Object>);
editor.putString("list", json);
editor.commit();

And then when I want to read this Json I just do:

SharedPreferences settings = getSharedPreferences(String name, MODE_MULTI_PROCESS);
String shared = settings.getString("list", null);
Gson gson = new Gson();
ArrayList<Object> temp = gson.fromJson(shared, new TypeToken<ArrayList<Object>>(){}.getType());

Upvotes: 0

kAnNaN
kAnNaN

Reputation: 3679

Levels.java

import java.util.List;


public class Levels {
    private Number id;
    private String name;
    private List sequence;

    public Number getId(){
        return this.id;
    }
    public void setId(Number id){
        this.id = id;
    }
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name;
    }
    public List getSequence() {
        return sequence;
    }
    public void setSequence(List sequence) {
        this.sequence = sequence;
    }
}

Songs.java

import java.util.List;

    public class Songs{
    private List<Levels> levels;

    public List<Levels> getLevels() {
        return levels;
    }

    public void setLevels(List<Levels> levels) {
        this.levels = levels;
    }

}

and use this to get the sequence list:

String json = new String("{\n    \"levels\": [\n        {\n            \"id\": 1,\n            \"name\": \"Sequence\",\n            \"sequence\": [\n                17,\n                1,\n                2\n            ]\n        },\n        {\n            \"id\": 2,\n            \"name\": \"Sequence\",\n            \"sequence\": [\n                17,\n                0,\n                1,\n                2,\n                4,\n                4,\n                5,\n                6\n            ]\n        }\n    ]\n}");

Gson g = new Gson();
Songs vc = (Songs)g.fromJson(json, Songs.class);
List test = vc.getLevels().get(0).getSequence();

Upvotes: 1

Related Questions