Skip to content Skip to sidebar Skip to footer

How To Convert String Value To Custom Model Object In Java?

I have one Model Object. In which, i have multiple values. I want to store this Values in SQLite. But data is large, so i want to store Direct Model object in databse. So i convert

Solution 1:

You should use GSON or similar libs for this.


Store to DB

For example If you use GSON

Personp=newPerson();
p.setname("xyz");
p.setage("18");
Gsongson=newGson();
StringpersonString= gson.toJson(p);

Now store this personString to DB.


Read from DB

Get back this object from database, read string from DB and convert it to object like below

StringpersonStringFromDB= READ_LOGIC_OF_DB;
Gsongson=newGson();
Personp= gson.fromJson(personStringFromDB, Person.class);

For more information, read GSON - Gson Example

Solution 2:

You can make Model Object serializable. You need to store the serialized object in SQLite. When you need it, you just get that serialized object from SOLite and deserialize it.

Post a Comment for "How To Convert String Value To Custom Model Object In Java?"