Java 将字符串值转换为自定义对象列表

Java 将字符串值转换为自定义对象列表,java,json,parsing,collections,java-11,Java,Json,Parsing,Collections,Java 11,我有一个字符串[{max=0.0,min=0.0,co=3.0},{max=0.0,min=0.0,co=3.0}] 我想将其转换为List,其中自定义对象是Java类 CustomObject{ Integer max; Integer min; Integer co; //getter setter } 有没有最佳的转换或转换方法?我不认为只有使用Java标准库就可以做到这一点。但如果您使用外部库,如,则非常简单: String str = "[{max=0.0, m

我有一个字符串
[{max=0.0,min=0.0,co=3.0},{max=0.0,min=0.0,co=3.0}]
我想将其转换为
List
,其中自定义对象是Java类

CustomObject{
   Integer max;
   Integer min;
   Integer co;
  //getter setter
}

有没有最佳的转换或转换方法?

我不认为只有使用Java标准库就可以做到这一点。但如果您使用外部库,如,则非常简单:

String str = "[{max=0.0, min=0.0, co=3.0},{max=0.0, min=0.0, co=3.0}]";

// Parse str to list of maps
List<Map<String, Double>> list = new Gson().fromJson(str, List.class);

// Convert list of maps to list of CustomObject
List<CustomObject> objects = list.stream().map(map -> {
    CustomObject obj = new CustomObject();
    obj.min = map.get("min").intValue();
    obj.max = map.get("max").intValue();
    obj.co = map.get("co").intValue();
    return obj;
}).collect(Collectors.toList());
String str=“[{max=0.0,min=0.0,co=3.0},{max=0.0,min=0.0,co=3.0}]”;
//将str解析为映射列表
List List=new Gson().fromJson(str,List.class);
//将地图列表转换为CustomObject列表
List objects=List.stream().map(map->{
CustomObject obj=新的CustomObject();
obj.min=map.get(“min”).intValue();
obj.max=map.get(“max”).intValue();
obj.co=map.get(“co”).intValue();
返回obj;
}).collect(Collectors.toList());

我不认为只有使用Java标准库就可以做到这一点。但如果您使用外部库,如,则非常简单:

String str = "[{max=0.0, min=0.0, co=3.0},{max=0.0, min=0.0, co=3.0}]";

// Parse str to list of maps
List<Map<String, Double>> list = new Gson().fromJson(str, List.class);

// Convert list of maps to list of CustomObject
List<CustomObject> objects = list.stream().map(map -> {
    CustomObject obj = new CustomObject();
    obj.min = map.get("min").intValue();
    obj.max = map.get("max").intValue();
    obj.co = map.get("co").intValue();
    return obj;
}).collect(Collectors.toList());
String str=“[{max=0.0,min=0.0,co=3.0},{max=0.0,min=0.0,co=3.0}]”;
//将str解析为映射列表
List List=new Gson().fromJson(str,List.class);
//将地图列表转换为CustomObject列表
List objects=List.stream().map(map->{
CustomObject obj=新的CustomObject();
obj.min=map.get(“min”).intValue();
obj.max=map.get(“max”).intValue();
obj.co=map.get(“co”).intValue();
返回obj;
}).collect(Collectors.toList());

它看起来像一个json字符串,但实际上不是。首先,您需要将其转换为json字符串,您可以使用正则表达式,然后使用Jackson。它看起来像json字符串,但实际上不是。首先,您需要将其转换为json字符串,可以使用正则表达式,然后使用Jackson。