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

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

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

Postby masterchris_99 » Tue Nov 23, 2010 8:26 am

Hello,

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
Code: Select all
[
    {
        "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:

Code: Select all
public class License {
   public String prog;
   public String name;
   public String computername;
   public String date;
   
   License() {
      this.computername = "";
      this.date = "";
      this.name = "";
      this.prog = "";
       // no-args constructor
   }
}


Code: Select all
           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
masterchris_99
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Nov 23, 2010 8:14 am

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 4 guests