gsonの助けを借りてjson文字列を逆シリアル化しようとしています。 gson.fromJsonている間、私は次のエラーを取得する:gson - > "クラスxyzのコンストラクタがありません"配列あり
No-args constructor for class xyz; does not exist. Register an InstanceCreator with Gson for this type to fix this problem
は私がInstanceCreateで動作するようにしようとしたが、私はこの実行を取得できませんでした。 私があなたを助けてくれることを願っています。私は( "[" と "]")最初と最後の文字をカットする必要がgsonに従って
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"
}
]
を 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();
}
よろしくクリス
それでも同じです。私はLicense []ではなく、Licenseでデシリアライズし、json文字列内にオブジェクトを1つだけ入れると動作します。 publicコンストラクタなしでも –
なぜ 'j = new JSONObject(JSONSerializedResources)'を使うのですか? –
これはgsonのチュートリアルの一部です –