Can you help me out. I am trying to save my Java Objects(Customer) in android currently I am following a book in my studies.
This is what I got, I have Customer Object that has a String and UUID. With these I am able to save them successfully through converting it with JSON. My problem is I want to add an ArrayList of Objects which contains(Double, and two strings) for each Customer. How can I save this ArrayList and retrieve it inside that JSON Object when I convert my Customer Object to json.
I researched but can't really understand it well. I found GSON but I am not sure whether this is the one I needed and don't know how to add this library to my project. Another is can I be able to put JSONArray into a JSON object? These are the two potential solutions that I found but not that sure. I'm trying to save this through file not SQLite. Is there anyway?
Here is the code for the Customer class
public class Customer {
//Constants for JSON
private static final String JSON_ID = "id";
private static final String JSON_NAME = "name";
private String mName;
private UUID mID;
private ArrayList<Item> mItems;
public Customer(){
mID = UUID.randomUUID();
mItems = new ArrayList<Item>();
}
//Loading Customer from JSON
public Customer(JSONObject jsonObject) throws JSONException{
mID = UUID.fromString(jsonObject.getString(JSON_ID));
if(jsonObject.has(JSON_NAME)){
mName = jsonObject.getString(JSON_NAME);
}
}
//JSON converter
public JSONObject toJSON() throws JSONException{
JSONObject json = new JSONObject();
json.put(JSON_ID, mID.toString());
json.put(JSON_NAME,mName);
if (!mItems.isEmpty()){
}
return json;
}
public UUID getmID() {
return mID;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
//Add item debt
public void addDebt(Item i){
mItems.add(i);
}
//Get List Debt
public ArrayList<Item> getmItems(){
return mItems;
}
public Item getItem(UUID id){
for (Item item : mItems){
if (item.getmItemID().equals(id)){
return item;
}
}
return null;
}
}
This is for the Item
public class Item {
private String mItemName;
private Date mDate;
private UUID mItemID;
private double mPrice;
public Item(){
mDate = new Date();
mItemID = UUID.randomUUID();
}
public UUID getmItemID() {
return mItemID;
}
public void setmItemID(UUID mItemID) {
this.mItemID = mItemID;
}
public String getmItemName() {
return mItemName;
}
public void setmItemName(String mItemName) {
this.mItemName = mItemName;
}
public String toString(){
return mItemName;
}
public Date getmDate() {
return mDate;
}
public void setmDate(Date mDate) {
this.mDate = mDate;
}
public double getmPrice() {
return mPrice;
}
public void setmPrice(double mPrice) {
this.mPrice = mPrice;
}
}