Nur Zitat.
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){
//-----------------------------
//jsonReader-Experiment
//-----------------------------
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("Numerischer Fehler:" + e.getCause().getMessage());
}else if (e.getCause() instanceof ParseException){
System.out.println("Datumseingabefehler:" + e.getCause().getMessage());
}
System.out.println("------------------------------------------->");
}
}
Es ist ein Erfolg ohne Schwierigkeiten. Der Teil, der den Typ übergibt, den Sie in Gson konvertieren möchten, ist der Schlüssel.
------------------------------------------->
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("]");
Wenn der Empfänger ein int-Typ ist, ist er übrigens "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("]");
Ich erhalte eine 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 analysiert aus der obersten Zeile, linkes Element auf json, Es wird eine Ausnahme ausgelöst, wenn die Konvertierung fehlschlägt. Gemäß diesem Experiment wird das Format von "JJJJ-MM-TT" und "JJJJMMTT" auf das Datum konvertiert. Eine Ausnahme tritt für "JJJJ / MM / TT" auf.
------------------------------------------->
Datumseingabefehler: Datum konnte nicht analysiert werden["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]]
------------------------------------------->
Interessanterweise wird hier 2020/2/30 auf 2020/3/1 umgestellt.
Es ist dieser Teil, der in Date konvertiert wird. # L78) Es sieht so aus. Ich denke, ich kann es hier anpassen. .. ..
Als ich es nachgeschlagen habe, in diesem Teil Es scheint, dass Sie das Datumsformat angeben können. Ich werde es versuchen.
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("------------------------------------------->");
Das Ergebnis ist eine erfolgreiche Perspektive!
------------------------------------------->
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]]
------------------------------------------->
Hört sich gut an. Sehr einfach zu bedienen! !!
Recommended Posts