更新时间:2023-01-16 07:47:55
您需要 Java Data 类作为 JSON 的精确模型.所以你的
You need your java Data class to be an exact model of the JSON. So your
{"data":{"translations":[{"translatedText":"Bonjour tout le monde"}]}}
变成:
class DataWrapper {
public Data data;
public static DataWrapper fromJson(String s) {
return new Gson().fromJson(s, DataWrapper.class);
}
public String toString() {
return new Gson().toJson(this);
}
}
class Data {
public List<Translation> translations;
}
class Translation {
public String translatedText;
}
随着对象模型变得更加复杂,org.json 风格的代码变得不可读,而 gson/jackson 风格的对象映射仍然只是普通的 java 对象.
As the object model gets more complicated the org.json style code veers towards unreadable whereas the gson/jackson style object mapping is still just plain java objects.