Java字符串到对象的List

时间:2016-07-29 20:03:39

标签: java android json string

如何从String中获取List对象,将新元素添加到列表并将其转回字符串。我使用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;
        }
    }

和我的代码更改List和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是'基本'字符串(这可能是空的),我不确定但可能存在问题,因为jakcson的字符串必须像[{element1},{element2}]而Json对象给出{ {1}}所以我放在那里{element1},{element2}

如果基本字符串为String toSave ="["+myJson.toString()+"]"我在行[{weightValue":"50","dateValue":"34234"}]中有错误:

myList=jsonmapper.readValue~~

有没有其他方法可以解决我的问题?

2 个答案:

答案 0 :(得分:1)

如果您希望将商店数据作为字符串为什么您要复杂化事物并涉及其他不必要的类和对象?

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

    a)选择数据(变量) delimiter1 &#34;:&#34;

    b)使用delimiter1

  2. 连接类记录(变量)的数据
  3. 使用String参数构造函数:

    a)使用先前使用的delimiter1

    拆分连接字符串

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

  4. 将静态方法设为:

    a)列表&lt;&gt;作为连接每个对象的参数toString()方法生成的字符串用 delimiter2 &#34;%&#34; 来实现这样的事情:

     "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 
    

答案 1 :(得分:0)

您可以使用Gson在您的类型和字符串之间切换。用法:

<强>的build.gradle

compile 'com.google.code.gson:gson:2.7'

从POJO到字符串:

Gson gson = new Gson();
String json = gson.toJson(weightData);

从字符串到POJO:

WeightDataJson newWeightData = gson.fromJson(json, WeightDataJson.class);

编辑:

使用ArrayList

List<WeightDataJson> list = new ArrayList<WeightDataJson>();
list.add(obj1);
list.add(obj2);
list.add(obj3);
..
Type type = new TypeToken<List<WeightDataJson>>() {}.getType();
String json = gson.toJson(list, type);
List<WeightDataJson> newWeightData = gson.fromJson(json, type);