Domanda

cerco di deserializzare una stringa JSON con l'aiuto di GSON. Mentre gson.fromJson ottengo il seguente errore:

  

Non-args costruttore per la classe xyz; non esiste. Pubblicare un InstanceCreator con GSON per questo tipo di risolvere questo problema

ho cercato di lavorare con un InstanceCreate, ma non ho avuto questo correre. Spero che tu mi possa aiutare.

JSON Stringa

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

]

in base alle GSON devo tagliare i primi e gli ultimi caratteri ( "[" e "]") secondo le http://www.jsonlint.com/ la stringa è con i caratteri corretti ...:? :

l'aspetto del codice del genere:

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

riguarda Chris

È stato utile?

Soluzione

provare a fare il costruttore pubblico, in modo che GSON può realmente accedervi.

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

ma dal momento che Java crea un costruttore di default, si potrebbe utilizzare:

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

Aggiornamento:

E 'davvero molto banale: JSONObject si aspetta un oggetto JSON "{..}". Si dovrebbe usare JSONArray che aspetta "[...]".

L'ho provato e funziona. Si dovrebbe comunque cambiare classe License come osservato in precedenza.

Altri suggerimenti

è incredibile ...

Ora ho provato non così

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

, ma direttamente in modo

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

con il mio vecchio stringa JSON originale con inizio e fine "[" e "]"

e funziona

Prova a rimuovere i costruttori se non si ha bisogno Ho provato ad utilizzare due classi Classe A con lista di Classe B

rimosso tutti i costruttori e bene solo ha lavorato per me.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top