动态将Json转换为Java Pojo

huangapple go评论76阅读模式
英文:

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 : 
         { 
              &quot;result&quot;: 
                {
                    &quot;1597696140&quot;: 70.32,
                    &quot;1597696141&quot;: 89.12,
                    &quot;1597696150&quot;: 95.32,
                }
         }

The pojo that I created is :


@JsonIgnoreProperties(ignoreUnknown = true)
public class ResultData {
  Map&lt;Long, Double&gt; resultMap;

  public ResultData(Map&lt;Long, Double&gt; 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(&quot;result&quot;).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:

{
  &quot;final_result&quot;: {
    &quot;result&quot;: {
      &quot;1597696140&quot;: 70.32,
      &quot;1597696141&quot;: 89.12,
      &quot;1597696150&quot;: 95.32
    }
  }
}

You can deserialise it to class:

@JsonRootName(&quot;final_result&quot;)
class ResultData {
    private Map&lt;Long, Double&gt; result;

    public Map&lt;Long, Double&gt; 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(&quot;./src/main/resources/test.json&quot;);

        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&lt;Long, Double&gt; resultData = objectMapper.readValue(resultData.getJSONObject(&quot;result&quot;).toString(), Map.class);
FinalResultData finaResultData = new FinalResultData(resultData);

huangapple
  • 本文由 发表于 2020年8月18日 05:45:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63459061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定