私はjsonファイルとサードパーティのクラスを与えられました:ディーラーとインターフェースIDealerAttributes(どちらも変更できません); (コードを単純にするために、パッケージ名とインポートを削除します)
JSONファイル
{{
"serviceURL": "com.mycompany.serviceURL"、
"dealerAttributes":[
{{
"言語": "言語0"、
"dealerAttributeName": "dealerAttributeName0"、
"updateDate":0
}、
{{
"言語": "言語1"、
"dealerAttributeName": "dealerAttributeName1"、
"updateDate":1
}
]
}
クラスディーラー{
プライベート文字列serviceURL;
プライベートリストdealerAttributes;
public Dealer(){
ディーラー属性=新しいArrayList();
}
//ゲッターとセッター...
}
パブリックインターフェイスIDealerAttributes{
public String getLanguage();
public String getDealerAttributeName();
public long getUpdateDate();
}
一度使用すると:
gson.fromJson(jsonObj.toString()、Dealer.class);
この行から例外が発生します:
json文字列をオブジェクトにアンマーシャリングする例外:com.google.gson.JsonParseException:JsonDeserializer com.google.gson.DefaultTypeAdapters $ CollectionTypeAdapter @ 60e26ffdがjsonオブジェクトの逆シリアル化に失敗しました[{"language": "language0"、 "dealerAttributeName": "dealerAttributeName0" 、"updateDate":0}、{"language": "language1"、 "dealerAttributeName": "dealerAttributeName1"、 "updateDate":1}]タイプjava.util.List
Dealer.class、IDealerAttributesに基づいてこのjsonファイルを読み取るにはどうすればよいですか?
ただし、クラスを1つ追加できます。たとえば、次のようにします。
パブリッククラスDealerAttributesはIDealerAttributesを実装します{
プライベート文字列言語;
プライベート文字列dealerAttributeName;
private long updateDate;
public DealerAttributes(){
}
//ゲッターとセッター...
}
私はjson/gsonを初めて使用するので、私を助けるための詳細な指示をお願いします。ありがとう。
[追加]Dealerクラスに100個のフィールドがあり、Dealerで使用/ネストされた別の100個のインターフェースがあるかどうかを検討してください。私は誰かがこの方法を使用した経験があるかどうかを考えています:(MyTypeはインターフェースです)
gson.registerTypeAdapter(MyType.class, new MyType());`