gson を使用して json 文字列をデシリアライズしようとしています。gson.fromJson 中に次のエラーが発生します。
クラス xyz の引数なしのコンストラクター。存在しません。この問題を解決するには、このタイプの InstanceCreator を Gson に登録します
InstanceCreate を使用しようとしましたが、これを実行できませんでした。あなたが私を助けてくれることを願っています。
JSON 文字列
[
{
"prog": "Name1",
"name": "Name2",
"computername": "Name3",
"date": "2010-11-20 19:39:55"
},
{
"prog": "Name1",
"name": "Name2",
"computername": "Name3",
"date": "2010-11-20 12:38:12"
}
]
gsonによると、http://www.jsonlint.com/によると、最初と最後の文字(「[」と「]」)をカットする必要があります。文字列は正しい文字で... :?:
コードは次のようになります。
public class License {
public String prog;
public String name;
public String computername;
public String date;
public License() {
this.computername = "";
this.date = "";
this.name = "";
this.prog = "";
// no-args constructor
}
}
String JSONSerializedResources = "json_string_from_above"
try
{
GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();
JSONObject j;
License[] lic = null;
j = new JSONObject(JSONSerializedResources);
lic = gson.fromJson(j.toString(), License[].class);
for (License license : lic) {
Toast.makeText(getApplicationContext(), license.name + " - " + license.date, Toast.LENGTH_SHORT).show();
}
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
よろしくクリス