Convert Custom class list to string and than Convert than String back to list in Java or Android.


Here code:-

List<Pets> list = new ArrayList<Pets>();
list.add(new Pets("jon","34"));
list.add(new Pets("Karan","12"));
list.add(new Pets("Meet","22"));
list.add(new Pets("Neel","49"));

//---------------------------Convert a list to String---------------------//

Gson gsonFilterList = new Gson();
String jsonInFilterList = gsonFilterList.toJson(list);
Log.e("Stepw", "jonInString:--- " + jsonInFilterList);


//---------------------------Convert a String to list ---------------------//

Type listType = new TypeToken<ArrayList<Pets>>() {
        }.getType();
List<Pets> yourClassList = new Gson().fromJson(jsonInFilterList , listType);


Log.e("yourClassList", String.valueOf(yourClassList.size()));



Comments

Popular posts from this blog

Lint found fatal errors while assembling a release target.

Windows 7 all Version 64-Bit iso file Download with crack activation. (Google Drive Link)

ViewModelProviders.of(this) is deprecated what to use instead ViewModelProviders.of(this) OR if you don't want to use ViewmodelFactory OR ViewModelProviders error.