[英]Java string to object's List
如何从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
是“基本”字符串(这可能为空),我不确定,但可能存在问题,因为jakcson的字符串必须像[{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])
还有另一种方法可以解决我的问题吗?
如果要将数据存储为String,为什么要使事情复杂化并涉及那些其他不必要的类和对象呢?
在您的类中重写方法toString()
a)选择数据(变量) 定界符1 “:”
b)使用delimiter1连接类记录(变量)的数据
使用String参数构造函数以:
a)使用先前使用的delimiter1拆分连接字符串
b)将split的结果[]分配给声明的变量
制作静态方法以:
a)使用List <>作为连接每个对象的参数,toString()方法使用delimiter2 “%”生成字符串,以实现以下目的:
"var1o1:var2o1%var1o2:var2o2%var1o3:var2o3" etc
b)用以下方法分割字符串:
ba/ delimieter2 - > then result each String pass to object to split with bb/ delimieter1 -> then result [] of split assign to variables
您可以使用Gson在类型和字符串之间切换。 用法:
build.gradle
compile 'com.google.code.gson:gson:2.7'
从POJO到String:
Gson gson = new Gson();
String json = gson.toJson(weightData);
从String到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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.