barmi barmi - 1 year ago 91
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){
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) {

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

JSONObject myJson=new JSONObject();
for(int i=0;i<myList.size();i++){
try {
} catch (JSONException e) {

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

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
and Json object gives
so I put there
String toSave ="["+myJson.toString()+"]"

If basic String is
I have an error in line

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 Source

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<>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download