How to convert String value to Custom Model Object in Java?
Asked Answered
P

3

13

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 model Object to string and store it into database.

Now, Problem is that how to convert this String value to Model Object. If you have any idea, please share that with Me.

For example,

Person p = new Person();
p.setname("xyz");
p.setage("18");`

String person=p.toString();

Now How to get this "person" string back to Person "p" model object.

This is my code.

ContentValues values = new ContentValues();
    String favorite_id = UUID.randomUUID().toString();
    values.put(EMuseumLocalData.KEY_FAVORITE_EXHIBITS_ID, favorite_id);
    values.put(EMuseumLocalData.KEY_EXHIBIT_SUBCATEGORY_ITEM_ID, Integer.parseInt(categoryByCustomerList.get(position).getSubCategoryItemID()));
    try {
        Gson gson = new Gson();
        String personString = gson.toJson(getAllCategory.get(position).toString());
        values.put(EMuseumLocalData.KEY_EXHIBIT_SUBCATEGORY_ITEM_DATA, personString);

        Gson gson1 = new Gson();
        CategoryByCustomer categoryByCustomer = gson1.fromJson(personString, categoryByCustomer.getName());
    } catch (JSONException e) {
        e.printStackTrace();
    }
Precognition answered 7/7, 2016 at 6:23 Comment(1)
Convert your person Object to Json object then convert Json object to String ... its work manBarmecide
J
22

You should use GSON or similar libs for this.


Store to DB

For example If you use GSON

Person p = new Person();
p.setname("xyz");
p.setage("18");
Gson gson = new Gson();
String personString = 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

String personStringFromDB = READ_LOGIC_OF_DB;
Gson gson = new Gson();
Person p = gson.fromJson(personStringFromDB, Person.class);

For more information, read GSON - Gson Example

Janeyjangle answered 7/7, 2016 at 6:34 Comment(6)
what is json in this line Person p = gson.fromJson(json, Person.class);Precognition
Updated that line. that was personStringFromDBJaneyjangle
If i so that then i get this error com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $ on this second line Gson gson1 = new Gson(); CategoryByCustomer categoryByCustomer = gson1.fromJson(personString, CategoryByCustomer.class);Precognition
Share your code. Are you editing string after conversion?Janeyjangle
Do not use toString() of you object. Problem is at 'String personString = gson.toJson(getAllCategory.get(position).toString());'. Instead-of doing it, just use 'String personString = gson.toJson(getAllCategory.get(position));'Janeyjangle
Let us continue this discussion in chat.Janeyjangle
I
1

Consider using a json string representation of the Model Object. There are many java libraries like Jackson, Gson etc., available to help you with serialization/deserialization part.

Here's a sample code to do this in Jackson

//For conversion of Person object(person) to json String:

String personJsonString = new com.fasterxml.jackson.databind.ObjectMapper().writeValueAsString(person);


//For conversion of json String back to Person object(person)

Person person = new com.fasterxml.jackson.databind.ObjectMapper().readValue(personJsonString, Person.class);
Indefinite answered 7/7, 2016 at 6:28 Comment(0)
P
0

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.

Plenum answered 7/7, 2016 at 6:29 Comment(2)
How to store this Serializable object into SQLite?Precognition
Serializable object is a stringPlenum

© 2022 - 2024 — McMap. All rights reserved.