Juste quote.
Gson Gson is a Java library that can be used to convert Java Objects into their JSON >representation. It can also be used to convert a JSON string to an equivalent Java object. >Gson can work with arbitrary Java objects including pre-existing objects that you do not have >source-code of.
There are a few open-source projects that can convert Java objects to JSON. However, most of >them require that you place Java annotations in your classes; something that you can not do if >you do not have access to the source-code. Most also do not fully support the use of Java >Generics. Gson considers both of these as very important design goals.
Person1.java
import java.util.Date;
public class Person1 {
private Integer age;
private String name;
private String noUse;
private Date target;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String noUse() {
return noUse;
}
public void noUse(String noUse) {
this.noUse = noUse;
}
public Date target() {
return target;
}
public void target(Date target) {
this.target = target;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Person1 [age=");
builder.append(age);
builder.append(", name=");
builder.append(name);
builder.append(", noUse=");
builder.append(noUse);
builder.append(", target=");
builder.append(target);
builder.append("]");
return builder.toString();
}
}
Ex1.java
public class Sample {
public static void main(String[] args){
//-----------------------------
//expérience jsonReader
//-----------------------------
StringBuilder jsonReader = new StringBuilder();
jsonReader.append("[");
jsonReader.append(" {\"age\":1,\"name\":\"Gson Taro\",\"target\":\"2020-02-10\"},");
jsonReader.append(" {\"age\":2,\"name\":\"Gson Taro\",\"target\":\"2020-02-30\"},");
jsonReader.append(" {\"age\":3,\"name\":\"Gson Taro\",\"target\":\"2020-02-12\"}");
jsonReader.append("]");
try {
Gson gsonReader = new Gson();
Type listType = new TypeToken<List<Person1>>(){}.getType();
List<Person1> p1 = gsonReader.fromJson(jsonReader.toString(), listType);
System.out.println("------------------------------------------->");
System.out.println("Person1#toString: " + p1);
System.out.println("------------------------------------------->");
} catch (Exception e) {
System.out.println(e.getCause());
System.out.println("------------------------------------------->");
if(e.getCause() instanceof NumberFormatException) {
System.out.println("Erreur numérique:" + e.getCause().getMessage());
}else if (e.getCause() instanceof ParseException){
System.out.println("Erreur de saisie de la date:" + e.getCause().getMessage());
}
System.out.println("------------------------------------------->");
}
}
C'est une réussite sans difficulté. La partie qui transmet le type que vous souhaitez convertir en Gson est la clé.
------------------------------------------->
Person1#toString: [Person1 [age=1, name=Gson Taro, noUse=null, target=Mon Feb 10 00:00:00 JST 2020], Person1 [age=2, name=Gson Taro, noUse=null, target=Sun Mar 01 00:00:00 JST 2020], Person1 [age=3, name=Gson Taro, noUse=null, target=Wed Feb 12 00:00:00 JST 2020]]
------------------------------------------->
test.java
StringBuilder jsonReader = new StringBuilder();
jsonReader.append("[");
jsonReader.append(" {\"age\":null,\"name\":\"Gson Taro\",\"target\":\"2020-02-10\"},");
jsonReader.append(" {\"age\":2,\"name\":\"Gson Taro\",\"target\":\"2020-02-30\"},");
jsonReader.append(" {\"age\":3,\"name\":\"Gson Taro\",\"target\":\"2020-02-12\"}");
jsonReader.append("]");
Au fait, si le destinataire est de type int, il sera "0".
------------------------------------------->
Person1#toString: [Person1 [age=null, name=Gson Taro, noUse=null, target=Mon Feb 10 00:00:00 JST 2020], Person1 [age=2, name=Gson Taro, noUse=null, target=Sun Mar 01 00:00:00 JST 2020], Person1 [age=3, name=Gson Taro, noUse=null, target=Wed Feb 12 00:00:00 JST 2020]]
------------------------------------------->
test.java
StringBuilder jsonReader = new StringBuilder();
jsonReader.append("[");
jsonReader.append(" {\"age\":test,\"name\":\"Gson Taro\",\"target\":\"2020-02-10\"},");
jsonReader.append(" {\"age\":2,\"name\":\"Gson Taro\",\"target\":\"2020-02-30\"},");
jsonReader.append(" {\"age\":3,\"name\":\"Gson Taro\",\"target\":\"2020-02-12\"}");
jsonReader.append("]");
J'obtiens une NumberFormatException.
test.java
jsonReader.append("[");
jsonReader.append(" {\"age\":1,\"name\":\"Gson Taro\",\"target\":\"2020-02-10\"},");
jsonReader.append(" {\"age\":2,\"name\":\"Gson Taro\",\"target\":\"20200230\"},");
jsonReader.append(" {\"age\":3,\"name\":\"Gson Taro\",\"target\":\"2020/02/12\"}");
jsonReader.append("]");
Gson analyse à partir de la ligne supérieure, élément de gauche sur json, Il lève une exception lorsque la conversion échoue. Selon cette expérience, le format "aaaa-MM-jj" et "aaaaMMjj" sera converti en date, Une exception se produira pour "aaaa / MM / jj".
------------------------------------------->
Erreur de saisie de la date: échec de l'analyse de la date["2020/02/12"]: Invalid number: _0
------------------------------------------->
test.java
StringBuilder jsonReader = new StringBuilder();
jsonReader.append("[");
jsonReader.append(" {\"age\":1,\"name\":\"Gson Taro\",\"target\":\"2020-02-10\"},");
jsonReader.append(" {\"age\":2,\"name\":\"Gson Taro\",\"target\":\"20200230\"},");
jsonReader.append(" {\"age\":3,\"name\":\"Gson Taro\",\"target\":\"20200212\"}");
jsonReader.append("]");
------------------------------------------->
Person1#toString: [Person1 [age=1, name=Gson Taro, noUse=null, target=Mon Feb 10 00:00:00 JST 2020], Person1 [age=2, name=Gson Taro, noUse=null, target=Sun Mar 01 00:00:00 JST 2020], Person1 [age=3, name=Gson Taro, noUse=null, target=Wed Feb 12 00:00:00 JST 2020]]
------------------------------------------->
Ici, il est intéressant de noter que 2020/2/30 est converti en 2020/3/1.
C'est cette partie qui est convertie en Date. # L78) Cela ressemble à ça. Je pense que je peux le personnaliser ici. .. ..
Quand je l'ai recherché, j'ai trouvé que cette partie Il semble que vous puissiez spécifier le format de la date. J'essaierai de l'utiliser.
test.java
jsonReader.append("[");
jsonReader.append(" {\"age\":1,\"name\":\"Gson Taro\",\"target\":\"2020-02-10\"},");
jsonReader.append(" {\"age\":2,\"name\":\"Gson Taro\",\"target\":\"20200230\"},");
jsonReader.append(" {\"age\":3,\"name\":\"Gson Taro\",\"target\":\"2020/02/12\"}");
jsonReader.append("]");
Gson gsonReader = new GsonBuilder().setDateFormat("yyyy/MM/dd").create();
Type listType = new TypeToken<List<Person1>>(){}.getType();
List<Person1> p1 = gsonReader.fromJson(jsonReader.toString(), listType);
System.out.println("------------------------------------------->");
System.out.println("Person1#toString: " + p1);
System.out.println("------------------------------------------->");
Le résultat est une perspective réussie!
------------------------------------------->
Person1#toString: [Person1 [age=1, name=Gson Taro, noUse=null, target=Mon Feb 10 00:00:00 JST 2020], Person1 [age=2, name=Gson Taro, noUse=null, target=Sun Mar 01 00:00:00 JST 2020], Person1 [age=3, name=Gson Taro, noUse=null, target=Wed Feb 12 00:00:00 JST 2020]]
------------------------------------------->
Ça m'a l'air bien. Très simple d'utilisation! !!
Recommended Posts