forked from chenssy89/jutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try to solve https://stackoverflow.com/q/48168302/1820217
Signed-off-by: Ein Verne <einverne@gmail.com>
- Loading branch information
Showing
7 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.jutils.gson; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by mi on 18-1-10. | ||
*/ | ||
public abstract class ResidenciaBase { | ||
@SerializedName("MOVIMIENTO") | ||
private List<Movimiento> movimiento; | ||
|
||
public List<Movimiento> getMovimiento() { | ||
return movimiento; | ||
} | ||
|
||
public void setMovimiento(List<Movimiento> movimiento) { | ||
this.movimiento = movimiento; | ||
} | ||
|
||
public static class Movimiento { | ||
|
||
@SerializedName("SALIDAS") | ||
private int salidas; | ||
|
||
@SerializedName("PERNOCTACIONES") | ||
private int pernoctaciones; | ||
|
||
@SerializedName("N_DIA") | ||
private String nDia; | ||
|
||
@SerializedName("ENTRADAS") | ||
private int entradas; | ||
|
||
public Movimiento(int salidas, int pernoctaciones, String nDia, int entradas) { | ||
this.salidas = salidas; | ||
this.pernoctaciones = pernoctaciones; | ||
this.nDia = nDia; | ||
this.entradas = entradas; | ||
} | ||
|
||
public int getSalidas() { | ||
return salidas; | ||
} | ||
|
||
public void setSalidas(int salidas) { | ||
this.salidas = salidas; | ||
} | ||
|
||
public int getPernoctaciones() { | ||
return pernoctaciones; | ||
} | ||
|
||
public void setPernoctaciones(int pernoctaciones) { | ||
this.pernoctaciones = pernoctaciones; | ||
} | ||
|
||
public String getnDia() { | ||
return nDia; | ||
} | ||
|
||
public void setnDia(String nDia) { | ||
this.nDia = nDia; | ||
} | ||
|
||
public int getEntradas() { | ||
return entradas; | ||
} | ||
|
||
public void setEntradas(int entradas) { | ||
this.entradas = entradas; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.jutils.gson; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
/** | ||
* Created by mi on 18-1-10. | ||
*/ | ||
public class ResidenciaPais extends ResidenciaBase { | ||
|
||
@SerializedName("ID_PAIS") | ||
private String idPais; | ||
|
||
public String getIdPais() { | ||
return idPais; | ||
} | ||
|
||
public void setIdPais(String idPais) { | ||
this.idPais = idPais; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.jutils.gson; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
/** | ||
* Created by mi on 18-1-10. | ||
*/ | ||
public class ResidenciaProv extends ResidenciaBase { | ||
@SerializedName("ID_PROVINCIA_ISLA") | ||
private String idProv; | ||
|
||
public String getIdProv() { | ||
return idProv; | ||
} | ||
|
||
public void setIdProv(String idProv) { | ||
this.idProv = idProv; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.jutils.gson; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by mi on 18-1-10. | ||
*/ | ||
public class SampleDeserializer implements JsonDeserializer<SampleObject> { | ||
@Override | ||
public SampleObject deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) | ||
throws JsonParseException { | ||
SampleObject sampleObject = new SampleObject(); | ||
SampleObject.Encuesta encuestaObject = new SampleObject.Encuesta(); | ||
SampleObject.ALOJAMIENTO alojamientoObject = new SampleObject.ALOJAMIENTO(); | ||
|
||
List<ResidenciaBase> baseList = new ArrayList<>(); | ||
JsonObject encuesta = jsonElement.getAsJsonObject().getAsJsonObject("ENCUESTA"); | ||
JsonObject alojamiento = encuesta.getAsJsonObject("ALOJAMIENTO"); | ||
JsonArray residencia = alojamiento.getAsJsonArray("RESIDENCIA"); | ||
|
||
for (JsonElement element : residencia) { | ||
if (element.getAsJsonObject().has("ID_PROVINCIA_ISLA")) { | ||
ResidenciaProv prov = new Gson().fromJson(element.getAsJsonObject().toString(), ResidenciaProv.class); | ||
baseList.add(prov); | ||
} | ||
if (element.getAsJsonObject().has("ID_PAIS")) { | ||
ResidenciaPais residenciaPais = new Gson().fromJson(element.getAsJsonObject().toString(), ResidenciaPais.class); | ||
baseList.add(residenciaPais); | ||
} | ||
} | ||
alojamientoObject.setResidencia(baseList); | ||
encuestaObject.setAlojamiento(alojamientoObject); | ||
|
||
JsonObject cabecera = encuesta.getAsJsonObject().getAsJsonObject("CABECERA"); | ||
SampleObject.Cabecera cabecera1 = new Gson().fromJson(cabecera.toString(), SampleObject.Cabecera.class); | ||
encuestaObject.setCabecera(cabecera1); | ||
sampleObject.setEncuesta(encuestaObject); | ||
return sampleObject; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.jutils.gson; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by mi on 18-1-10. | ||
*/ | ||
public class SampleObject { | ||
@SerializedName("ENCUESTA") | ||
private Encuesta encuesta; | ||
|
||
public Encuesta getEncuesta() { | ||
return encuesta; | ||
} | ||
|
||
public void setEncuesta(Encuesta encuesta) { | ||
this.encuesta = encuesta; | ||
} | ||
|
||
public static class Encuesta { | ||
@SerializedName("ALOJAMIENTO") | ||
private ALOJAMIENTO alojamiento; | ||
|
||
@SerializedName("CABECERA") | ||
private Cabecera cabecera; | ||
|
||
public ALOJAMIENTO getAlojamiento() { | ||
return alojamiento; | ||
} | ||
|
||
public void setAlojamiento(ALOJAMIENTO alojamiento) { | ||
this.alojamiento = alojamiento; | ||
} | ||
|
||
public Cabecera getCabecera() { | ||
return cabecera; | ||
} | ||
|
||
public void setCabecera(Cabecera cabecera) { | ||
this.cabecera = cabecera; | ||
} | ||
} | ||
|
||
public static class ALOJAMIENTO { | ||
@SerializedName("RESIDENCIA") | ||
private List<ResidenciaBase> residencia; | ||
|
||
public List<ResidenciaBase> getResidencia() { | ||
return residencia; | ||
} | ||
|
||
public void setResidencia(List<ResidenciaBase> residencia) { | ||
this.residencia = residencia; | ||
} | ||
} | ||
|
||
public class Cabecera { | ||
@SerializedName("FECHA_REFERENCIA") | ||
private FECHA_REFERENCIA fecha_referencia; | ||
|
||
@SerializedName("NOMBRE_ESTABLECIMIENTO") | ||
private String nombre_establecimiento; | ||
|
||
public FECHA_REFERENCIA getFecha_referencia() { | ||
return fecha_referencia; | ||
} | ||
|
||
public void setFecha_referencia(FECHA_REFERENCIA fecha_referencia) { | ||
this.fecha_referencia = fecha_referencia; | ||
} | ||
|
||
public String getNombre_establecimiento() { | ||
return nombre_establecimiento; | ||
} | ||
|
||
public void setNombre_establecimiento(String nombre_establecimiento) { | ||
this.nombre_establecimiento = nombre_establecimiento; | ||
} | ||
} | ||
|
||
public class FECHA_REFERENCIA { | ||
private int MES; | ||
private int ANYO; | ||
|
||
public int getMES() { | ||
return MES; | ||
} | ||
|
||
public void setMES(int MES) { | ||
this.MES = MES; | ||
} | ||
|
||
public int getANYO() { | ||
return ANYO; | ||
} | ||
|
||
public void setANYO(int ANYO) { | ||
this.ANYO = ANYO; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.jutils.gson; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by mi on 18-1-8. | ||
*/ | ||
public class GsonTest { | ||
|
||
Gson gson = new GsonBuilder().registerTypeAdapter(SampleObject.class, new SampleDeserializer()).create(); | ||
|
||
@Test | ||
public void createGson() { | ||
String demoStr = "{\n" + | ||
" \"ENCUESTA\": {\n" + | ||
" \"ALOJAMIENTO\": {\n" + | ||
" \"RESIDENCIA\": [\n" + | ||
" {\n" + | ||
" \"ID_PROVINCIA_ISLA\": \"ES511\",\n" + | ||
" \"MOVIMIENTO\": [\n" + | ||
" {\n" + | ||
" \"SALIDAS\": 0,\n" + | ||
" \"PERNOCTACIONES\": 3,\n" + | ||
" \"N_DIA\": \"08\",\n" + | ||
" \"ENTRADAS\": 3\n" + | ||
" }\n" + | ||
" ]\n" + | ||
" },\n" + | ||
" {\n" + | ||
" \"MOVIMIENTO\": [\n" + | ||
" {\n" + | ||
" \"SALIDAS\": 0,\n" + | ||
" \"PERNOCTACIONES\": 3,\n" + | ||
" \"N_DIA\": \"06\",\n" + | ||
" \"ENTRADAS\": 3\n" + | ||
" }\n" + | ||
" ],\n" + | ||
" \"ID_PAIS\": \"CHN\"\n" + | ||
" }\n" + | ||
" ]\n" + | ||
" },\n" + | ||
" \"CABECERA\": {\n" + | ||
" \"FECHA_REFERENCIA\": {\n" + | ||
" \"MES\": 11,\n" + | ||
" \"ANYO\": 2017\n" + | ||
" },\n" + | ||
" \"NOMBRE_ESTABLECIMIENTO\": \"UNKNOWN\"\n" + | ||
"\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}"; | ||
SampleObject sampleObject = gson.fromJson(demoStr, SampleObject.class); | ||
System.out.println(sampleObject); | ||
} | ||
} |