masterchris_99
masterchris_99

Reputation: 2723

gson --> "No-args constructor for class xyz" with arrays

i try to deserialize a json string with the help of gson. While gson.fromJson I get the following error:

No-args constructor for class xyz; does not exist. Register an InstanceCreator with Gson for this type to fix this problem

I tried to work with an InstanceCreate but I didn't get this running. I hope you can help me.

JSON String

[
{
    "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"
}

]

according to gson I have to cut the first and last chars ("[" and "]") according to http://www.jsonlint.com/ the string is with the chars correct... :?:

the code looks like that:

    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();
         }

regards Chris

Upvotes: 0

Views: 4597

Answers (3)

gibsosmart
gibsosmart

Reputation: 137

Try removing the constructors if you don't need I tried using two classes Class A having list of Class B

Removed all the constructors and just worked fine for me.

Upvotes: -1

Peter Knego
Peter Knego

Reputation: 80330

Try making your constructor public, so that gson can actually access it.

public License() {
  this.computername = "";
  this.date = "";
  this.name = "";
  this.prog = "";
  // no-args constructor
}

but since Java creates a default constructor, you could just use:

public class License {
    public String prog = "";
    public String name = "";
    public String computername = "";
    public String date = "";
}

Update:

It's really quite trivial: JSONObject expects a Json object "{..}". You should use JSONArray which expects "[...]".

I tried it and it works. You should still change License class as noted above.

Upvotes: 2

masterchris_99
masterchris_99

Reputation: 2723

it is unbelievable...

now I tried it not so

j = new JSONObject(JSONSerializedResources);
lic = gson.fromJson(j.toString(), License[].class);

but directly so

lic = gson.fromJson(JSONSerializedResources, License[].class);

with my old original json string with starting and ending "[" & "]"

and it works

Upvotes: 0

Related Questions