0
ための複数の値とGsonの使用:私はこのJSONコードを持っているオブジェクト
{
"term" : {
"PrincipalTranslations" : {
"0" : {
termine:"casa",
traduzione:"home"
}
"1" :{
termine:"testa",
traduzione:"head"
}
"2" :{
termine:"dito",
traduzione:"finger"
}
}
}
}
どのようにオブジェクト0、1、2を逆シリアル化できますか? オブジェクト0,1,2の代わりにオブジェクト "ゼロ"(そして停止)を書いた場合、それは機能します! 私はこの実装を使用しました:あなたがオブジェクトにゼロを呼び出したい場合は
public class Item {
private term term;
public term getTERM() {
return term;
}
}
public class term {
private PrincipalTranslations PrincipalTranslations;
public PrincipalTranslations getPrincipalTranslations() {
return PrincipalTranslations;
}
}
public class PrincipalTranslations {
private zero zero;
public zero getZero() {
return zero;
}
}
public class zero {
private String termine;
public String gettermine() {
return termine;
}
}
ので、それを使用し、それはあなたよりも、(正しい方法で)「カサ」
public class MainClass {
public static void main(String[] args) throws IOException {
FileReader reader = new FileReader("/home/peppe/test_ff");
Gson gson = new GsonBuilder().create();
Item p = gson.fromJson(reader, Item.class);
System.out.print(p.getTERM().getPrincipalTranslations().getZero().gettermine());
reader.close();
}
}
あなたの構造に間違いがあると思います。 PrincipalTranslationsには2つのフィールド(termineとtraduzione)を持つ項目の配列が含まれていることを理解しています。私があなたのクラス宣言を正しく見直したら、便利なコードを投稿することができます。 – Juvanis