barmi barmi - 4 months ago 8
Android Question

Java string to object's List

How is it possible to get List object from String, add new element to list and turn it back to string. I use Json and jackson, created class for list's element:

public class WeightDataJson {
private String weightValue;
private String dateValue;
public WeightDataJson(String weightValue,String dateValue){
this.weightValue=weightValue;
this.dateValue=dateValue;
}
public String getWeightValue() {
return weightValue;
}
public void setWeightValue(String weightValue) {
this.weightValue = weightValue;
}
public String getDateValue() {
return dateValue;
}
public void setDateValue(String dateValue) {
this.dateValue = dateValue;
}
}


and my code for change List and String:

String myWeightData = preferencesWeight.get();
List<WeightDataJson> myList = new Vector<WeightDataJson>();
ObjectMapper jsonMapper = new ObjectMapper();
try {
myList = jsonMapper.readValue(myWeightData, new TypeReference<List<WeightDataJson>>(){});
} catch (IOException e) {
e.printStackTrace();
}

WeightDataJson newWeightData = new WeightDataJson(editText.getText().toString(),"213123");
myList.add(newWeightData);

JSONObject myJson=new JSONObject();
for(int i=0;i<myList.size();i++){
try {
myJson.put("weightValue",myList.get(i).getWeightValue());
myJson.put("dateValue",myList.get(i).getDateValue());
} catch (JSONException e) {
e.printStackTrace();
}
}

String toSave ="["+myJson.toString()+"]";
Toast.makeText(getActivity(),"zapisuje: "+toSave,Toast.LENGTH_LONG).show();
preferencesWeight.save(toSave);
WeightDataProvider weightDataProvider = new WeightDataProvider(addIcon,deleteIcon,editText.getText().toString(),"213123");
weightAdapter.add(weightDataProvider);


myWeightData
is 'basic' String (this is possible that it is empty), I am not sure but probably there is problem because String for jakcson has to be like
[{element1},{element2}]
and Json object gives
{element1},{element2}
so I put there
String toSave ="["+myJson.toString()+"]"
.

If basic String is
[{weightValue":"50","dateValue":"34234"}]
I have an error in line
myList=jsonmapper.readValue~~
:

07-30 03:59:04.407 11837-11837/com.plan.aplikacjamobilna W/System.err: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.plan.aplikacjamobilna.WeightDataJson]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
07-30 03:59:04.407 11837-11837/com.plan.aplikacjamobilna W/System.err: at [Source: [{"weightValue":"14","dateValue":"213123"}]; line: 1, column: 3] (through reference chain: java.util.ArrayList[0])


Is there another way to solve my problem?

Answer

If you want store data as String Why are you complicating things and involving other unnecessary classes and object of those ?

  1. override method toString() in your class

    a) chose data delimiter

    b) concatenate data for Class records (variables)

  2. make constructor with String parameter

    a) split concatenated string with delimiter

    b) assign to declared variables

  3. make static methods to convert

    a) List<> - > concatenated twice string (first time for class object variables, second time for array elements [class object concatenated variables Strings])

     var1o1:var2o1|var1o2:var2o2|var1o3:var2o3| etc
    

    b) above string -> List<>