Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I Json (or Gson) a class like "objects to string" and after "string to objects"
    primarykey
    data
    text
    <p>My question is about an error occurred when I use my followed class:</p> <pre><code>public class ClassePai { private int ID; private String Nome; private ArrayList&lt;ClasseFilho&gt; listaParentes; public ClassePai( int id, String nome){ this.ID = id; this.Nome = nome; listaParentes = new ArrayList&lt;ClasseFilho&gt;(); } public void addParente(ClasseFilho classeFilho) { listaParentes.add(classeFilho); } public ClasseFilho getParente( int index ){ return listaParentes.get(index); } public int length(){ return listaParentes.size(); } public String Nome(){ return this.Nome; } public int ID(){ return this.ID; } } </code></pre> <p>and this "son" class:</p> <pre><code>public class ClasseFilho { private String Nome; private String Grau; public ClasseFilho( String nome, String grau ){ this.Nome = nome; this.Grau = grau; } public String Nome(){ return this.Nome; } public String Grau(){ return this.Grau; } } </code></pre> <p>This is my Activity:</p> <pre><code>public class TesteClasseActivity extends Activity { private ClassePai cp; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); cp = new ClassePai( 1, "Junior" ); cp.addParente( new ClasseFilho( "Vinicius", "filho" ) ); cp.addParente( new ClasseFilho( "Luciene", "namorada" ) ); //old call returning error //Toast.makeText(getApplicationContext(), cp.length(), Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), String.valueOf( cp.length() ), Toast.LENGTH_SHORT).show(); cp.addParente( new ClasseFilho( "Veraldo", "pai" ) ); cp.addParente( new ClasseFilho( "Sônia", "mãe" ) ); Toast.makeText(getApplicationContext(), String.valueOf( cp.length() ), Toast.LENGTH_SHORT).show(); ( (TextView) findViewById(R.id.edit) ).setText( cp.Nome() ); Button bt = (Button) findViewById(R.id.button); bt.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { Toast.makeText(getApplicationContext(), cp.getParente(1).Nome(), Toast.LENGTH_SHORT).show(); } }); //my try of doing this, but don't work JSONObject obj = new JSONObject(); obj.put("teste", cp); // BUT unfortunatelly // // obj.toString() returns this: // // {"teste":"my.package.name.ClassePai@40516650"} // // BUT... //wondering on net, I've deceided to try Gson: //first part: object to string Gson obj = new Gson(); obj.toJson(cp); String obj_in_xml = obj.toString(); Toast.makeText( getApplicationContext() , obj_in_xml, Toast.LENGTH_SHORT).show(); //no error, appear works fine, but... //second part: string to object Gson obj2 = new Gson(); ClassePai new_cp = obj2.fromJson( obj_in_xml, ClassePai.class); Toast.makeText( getApplicationContext() , new_cp.Nome(), Toast.LENGTH_SHORT).show(); //give me an error com.google.gson.stream.MalformedJsonException //and I could't find my properties in obj.toString(), but only codes like: // //adapter=com.google.gson.internal.bind.TypeAdapters$7@40529278] // // and don't find any of the object's string properties in the obj.toString() string // // what I'm doing wrong???? } } </code></pre> <p>How can I do to save this object with children in a text format, and after load from String to the objects again?</p> <p>Please help me!!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload