android - Display data parsed from Json with Gson in a recyclerview -
i have json string:
{ "status": "true", "result": { "rows": { "row": { "status": true, "subareas": [ { "nome": "associacao utente", "id": 9, "grafs": { "rows": { "row": { "id": 6, "nome": "associacao utente", "tipo": "pie", "serv": "mv_as_utente_por_negocio", "periodo": "ano" } } } }, { "nome": "chaves", "id": 60, "grafs": { "rows": [ { "id": 35, "nome": "chaves criados por ano", "tipo": "linha", "serv": "mv_assoc_total_chaves", "periodo": "ano" }, { "id": 592, "nome": "chaves associado ao user portal", "tipo": "bar", "serv": "mv_assoc_user_chaves", "periodo": "todos" }, { "id": 593, "nome": "chaves associado ao negocios", "tipo": "bar", "serv": "mv_assoc_chaves", "periodo": "todos" } ] } } ] } } } }
which deserialized pojo gson. below show deserializer:
example.java
public class example { private string status; private result result; public string getstatus() { return status; } public void setstatus(string status) { status = status; } public result getresult() { return result; } public void setresult(result result) { result = result; } @override public string tostring() { return "example [status=" + status + ", result=" + result + "]"; }
result.java
public class result { private rows rows; public rows getrows() { return rows; } public void setrows(rows rows) { this.rows = rows; } @override public string tostring() { return "result [rows=" + rows + "]"; }
rows.java
public class rows { private row row; public row getrow() { return row; } public void setrow(row row) { this.row = row; } @override public string tostring() { return "rows [row=" + row + "]"; }
row.java
public class row { private boolean status; private list<subarea> subareas = new arraylist<>(); public boolean getstatus() { return status; } public void setstatus(boolean status) { this.status = status; } public list<subarea> getsubareas() { return subareas; } public void setsubareas(list<subarea> subareas) { this.subareas = subareas; } @override public string tostring() { return "row [status=" + status + ", subareas=" + subareas + "]"; }
subarea.java
public class subarea { private string nome; private integer id; private grafs grafs; public string getnome() { return nome; } public void setnome(string nome) { this.nome = nome; } public integer getid() { return id; } public void setid(integer id) { this.id = id; } public grafs getgrafs() { return grafs; } public void setgrafs(grafs grafs) { this.grafs = grafs; } @override public string tostring() { return "subarea [nome=" + nome + ", id=" + id + ", grafs=" + grafs + "]"; }
grafs.java
public class grafs { private row_[] rows; public list<row_> getrows() { return new linkedlist<row_>( arrays.aslist(rows)); } public void setrows(list<row_> rows) { this.rows = (row_[]) rows.toarray(); } @override public string tostring() { return "grafs [rows=" + rows[0] + "]"; } public static class row_deserializer implements jsondeserializer<row_[]>{ @override public row_[] deserialize(jsonelement json, type typeoft, jsondeserializationcontext context) throws jsonparseexception{ if(json instanceof jsonarray){ return new gson().fromjson(json, row_[].class); } system.out.println("ola"); rows1 child = context.deserialize(json, rows1.class); system.out.println(child.getrow().getid()); return new row_[] {child.getrow()}; } }
row_.java
public class row_ { private integer id; private string nome; private string serv; private string periodo; public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getnome() { return nome; } public void setnome(string nome) { this.nome = nome; } public string getserv() { return serv; } public void setserv(string serv) { this.serv = serv; } public string getperiodo() { return periodo; } public void setperiodo(string periodo) { this.periodo = periodo; } @override public string tostring() { return "row_ [id=" + id + ", nome=" + nome + ", serv=" + serv + ", periodo=" + periodo + "]"; }
this working fine deserialize json. need doing display of data in recyclerview can produce following output. want display each subarea grafs name. first subarea has 1 grafs , second has grafs can see in output.
you can parse json line:
new gson().fromjson(json, example.class);
this simple way.
Comments
Post a Comment