[英]Convert values from json string to object
I have a JSON
payload of the format, with each value a list with single map element:我有一个格式的JSON
有效负载,每个值都是一个带有单个 map 元素的列表:
{
"redundant_str_1": [
{
"attr_1": "val1",
"attr_2": "val2"
}
],
"rendundant_str_2": [
{
"attr_1": "val4",
"attr_2": "val3"
}
]
}
Model is: Model 是:
public class MyObj {
private String attr_1;
private String attr_2;
}
How can I map above response to List<MyObj>
by neglecting the keys and taking only element from lists?如何通过忽略键并仅从列表中获取元素来响应List<MyObj>
上面的 map?
String json = "{\"redundant_str_1\": [{\"attr_1\": \"val1\", \"attr_2\": \"val2\"}], \"rendundant_str_2\": [{\"attr_1\": \"val4\", \"attr_2\": \"val3\"}]}";
ObjectMapper objectMapper = new ObjectMapper();
List<MyObj> myObjList = new ArrayList<>();
ObjectNode objectNode = (ObjectNode) objectMapper.readTree(json);
for (Iterator<JsonNode> it = objectNode.elements(); it.hasNext(); ) {
ArrayNode arrayNode = (ArrayNode) it.next();
MyObj myObj = objectMapper.treeToValue(arrayNode.get(0),MyObj.class);
myObjList.add(myObj);
}
System.out.println("List : " + myObjList);
Output: Output:
List : [MyObj{attr_1='val1', attr_2='val2'}, MyObj{attr_1='val4',attr_2='val3'}]
You can use Jackson
(add dependencies to jackson-core
and jackson-databind
):您可以使用Jackson
(将依赖项添加到jackson-core
和jackson-databind
):
String json = "[" +
"{\"attr_1\": \"val1\", \"attr_2\": \"val2\"}, " +
"{\"attr_1\": \"val4\", \"attr_2\": \"val3\"}"
+ "]";
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
List<MyObj> list = Arrays.asList(mapper.readValue(json, MyObj[].class));
Or, if you add to MyObj
setters on fields or make them public there will be no needed the row:或者,如果您在字段上添加MyObj
设置器或将它们公开,则不需要该行:
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
Your response is of form Map<String, List<MyObj>>
.您的回复形式为Map<String, List<MyObj>>
。 You can deserialise it to given type and after this convert to a List<MyObj>
.您可以将其反序列化为给定类型,然后转换为List<MyObj>
。
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import lombok.Data;
import lombok.ToString;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class MyObjApp {
public static void main(String[] args) throws IOException {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = JsonMapper.builder().build();
TypeReference<Map<String, List<MyObj>>> respType = new TypeReference<Map<String, List<MyObj>>>() {};
Map<String, List<MyObj>> response = mapper.readValue(jsonFile, respType);
List<MyObj> myObjs = response.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
System.out.println(myObjs);
}
}
@Data
@ToString
class MyObj {
private String attr_1;
private String attr_2;
}
Above code prints:上面的代码打印:
[MyObj(attr_1=val1, attr_2=val2), MyObj(attr_1=val4, attr_2=val3)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.