How can I use “objects to string” and after “string to objects” using Json (or Gson) in Android?

Up vote 0 down vote favorite share g+ share fb share tw.

My question is about an error occurred when I use my followed class: public class ClassePai { private int ID; private String Nome; private ArrayList listaParentes; public ClassePai( int id, String nome){ this. ID = id; this. Nome = nome; listaParentes = new ArrayList(); } 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; } } and this "son" class: 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; } } This is my Activity: 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(); } }); Now I need save the cp object to String, and then vice-versa: //my try of doing this with Json, but don't work JSONObject obj = new JSONObject(); obj. Put("teste", cp); // BUT unfortunatelly // // obj.toString() returns this: // // {"teste":"my.package.name.

ClassePai@40516650"} // Wondering on net, I've decided try use 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? How can I save this object with children in a text format, and after load from String to the objects again? Please help me!

Android string json object gson link|improve this question asked Apr 4 at 16:35alijunior12.

Verify that you are reading the json in the good order. Maybe you parse a JSONArray and not a JSONObject? – haythem souissi Apr 4 at 16:41 you can add all the log cat message it will give us the line where you got the problem – haythem souissi Apr 4 at 16:42.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions