Java字符串到对象的列表

巴米

如何从String中获取List对象,如何向列表中添加新元素,然后将其转换回String。我使用Json和jackson,为list的元素创建了类:

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

和我的代码更改列表和字符串:

                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是'basic'字符串(这可能是空的),我不确定,但是可能存在问题,因为jakcson的String必须像[{element1},{element2}]Json对象一样{element1},{element2}所以我放在那里String toSave ="["+myJson.toString()+"]"

如果基本字符串是[{weightValue":"50","dateValue":"34234"}]我在行中有一个错误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])

还有另一种方法可以解决我的问题吗?

ceph3us

如果要将数据存储为String,为什么要使事情复杂化并涉及那些其他不必要的类和对象呢?

  1. 在您的类中重写方法toString()

    a)选择数据(变量)定界符1 “:”

    b)使用delimiter1连接类记录(变量)的数据

  2. 使带有String参数的构造函数能够:

    a)使用先前使用的delimiter1拆分连接字符串

    b)将split的结果[]分配给声明的变量

  3. 制作静态方法以:

    a)使用List <>作为连接每个对象的参数,toString()方法使用delimiter2 “%”生成字符串,以实现以下目的:

     "var1o1:var2o1%var1o2:var2o2%var1o3:var2o3" etc
    

    b)用以下方法分割字符串:

    b.a/ delimieter2  - > then result each String pass to object to split with
    
    b.b/ delimieter1 -> then result [] of split assign to variables 
    

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章