英文:
Dynamic Json To Java Pojo
问题
我想将以下 JSON 映射到 Java 中的 POJO 类。在下面显示的代码片段中,result
是一个 JSON 对象,其值是另一个 JSON 对象,即一个映射。我尝试将其转换为 POJO,但是失败了。result
映射中的键是动态的,我无法预先猜测它们。
我创建的 POJO 类是:
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResultData {
Map<Long, Double> resultMap;
public ResultData(Map<Long, Double> resultMap) {
this.resultMap = resultMap;
}
public ResultData() {
}
@Override
public String toString() {
return super.toString();
}
}
尝试使用 ObjectMapper 创建 POJO 类时:
ObjectMapper objectMapper = new ObjectMapper();
ResultData resultData = objectMapper.readValue(resultData.getJSONObject("result").toString(), ResultData.class);
我可能在这里做错了什么?
英文:
I want to map the following json to a pojo in Java. In the snippet shown below, result
is a Json object, whose value is another json object which is a map. I tried converting this to a Pojo, but it failed. The keys in the result
map are dynamic, and I cannot guess them prior.
final_result :
{
"result":
{
"1597696140": 70.32,
"1597696141": 89.12,
"1597696150": 95.32,
}
}
The pojo that I created is :
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResultData {
Map<Long, Double> resultMap;
public ResultData(Map<Long, Double> resultMap) {
this.resultMap = resultMap;
}
public ResultData() {
}
@Override
public String toString() {
return super.toString();
}
}
Upon trying to create the pojo using ObjectMapper :
ObjectMapper objectMapper = new ObjectMapper();
ResultData resultData = objectMapper.readValue(resultData.getJSONObject("result").toString(), ResultData.class);
What am I possible doing wrong here ?
答案1
得分: 1
假设你的 JSON
负载如下所示:
{
"final_result": {
"result": {
"1597696140": 70.32,
"1597696141": 89.12,
"1597696150": 95.32
}
}
}
你可以将其反序列化为以下类:
@JsonRootName("final_result")
class ResultData {
private Map<Long, Double> result;
public Map<Long, Double> getResult() {
return result;
}
@Override
public String toString() {
return result.toString();
}
}
如下所示:
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException {
File jsonFile = new File("./src/main/resources/test.json");
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
ResultData resultData = mapper.readValue(jsonFile, ResultData.class);
System.out.println(resultData);
}
}
上述代码输出:
{1597696140=70.32, 1597696141=89.12, 1597696150=95.32}
英文:
Assume, your JSON
payload looks like below:
{
"final_result": {
"result": {
"1597696140": 70.32,
"1597696141": 89.12,
"1597696150": 95.32
}
}
}
You can deserialise it to class:
@JsonRootName("final_result")
class ResultData {
private Map<Long, Double> result;
public Map<Long, Double> getResult() {
return result;
}
@Override
public String toString() {
return result.toString();
}
}
Like below:
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException {
File jsonFile = new File("./src/main/resources/test.json");
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
ResultData resultData = mapper.readValue(jsonFile, ResultData.class);
System.out.println(resultData);
}
}
Above code prints:
{1597696140=70.32, 1597696141=89.12, 1597696150=95.32}
答案2
得分: 0
将JSONObject转换为Map,并将该Map设置到pojo字段中,解决了这个问题,而且不需要编写自定义的反序列化器。
Map<Long, Double> resultData = objectMapper.readValue(resultData.getJSONObject("result").toString(), Map.class);
FinalResultData finaResultData = new FinalResultData(resultData);
英文:
Converting the JSONObject to Map and setting the map to the pojo field, solved the issue and didn't lead me to writing a custom deserializer.
Map<Long, Double> resultData = objectMapper.readValue(resultData.getJSONObject("result").toString(), Map.class);
FinalResultData finaResultData = new FinalResultData(resultData);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论