将JSON转换(反序列化)为Java POJO Jackson。

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

Convert (Deserializing) JSON to Java POJO Jackson

问题

import java.util.Map;

public class Alphavantage {
    public MetaData metaData;
    public Map<String, TimeSeriesDaily> timeSeriesDaily;
}

public class MetaData {
    public String _1Information;
    public String _2Symbol;
    public String _3LastRefreshed;
    public String _4OutputSize;
    public String _5TimeZone;
}

public class TimeSeriesDaily {
    public String _1open;
    public String _2high;
    public String _3low;
    public String _4close;
    public String _5volume;
}

public static Map<Alphavantage, Map<String, Object>> getAlphaData() {
    RestAssured.baseURI = uri;
    RequestSpecification request = RestAssured.given().log().all();
    Response response = request.get(uri + "/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=demo");
    String jsonResponse = response.getBody().asString();

    ObjectMapper mapper = new ObjectMapper();
    Map<Alphavantage, Map<String, Object>> data = mapper.readValue(jsonResponse, new TypeReference<Map<Alphavantage, Map<String, Object>>>() {});
    return data;
}
英文:

How to convert below JSON to java HashMap
What will be the POJO
How map JSON object with Jackson

*{
    &quot;Meta Data&quot;: {
        &quot;1. Information&quot;: &quot;Daily Prices (open, high, low, close) and Volumes&quot;,
        &quot;2. Symbol&quot;: &quot;IBM&quot;,
        &quot;3. Last Refreshed&quot;: &quot;2020-08-14&quot;,
        &quot;4. Output Size&quot;: &quot;Compact&quot;,
        &quot;5. Time Zone&quot;: &quot;US/Eastern&quot;
    },
    &quot;Time Series (Daily)&quot;: {
        &quot;2020-08-14&quot;: {
            &quot;1. open&quot;: &quot;124.2000&quot;,
            &quot;2. high&quot;: &quot;125.5600&quot;,
            &quot;3. low&quot;: &quot;123.9100&quot;,
            &quot;4. close&quot;: &quot;125.2700&quot;,
            &quot;5. volume&quot;: &quot;2963753&quot;
        },
        &quot;2020-08-13&quot;: {
            &quot;1. open&quot;: &quot;125.9600&quot;,
            &quot;2. high&quot;: &quot;126.3900&quot;,
            &quot;3. low&quot;: &quot;124.7700&quot;,
            &quot;4. close&quot;: &quot;125.0300&quot;,
            &quot;5. volume&quot;: &quot;3171258&quot;
        }
    }
}

This is POJO of Alphavantage:

public class Alphavantage {
    public MetaData metaData;
    public TimeSeriesDaily timeSeriesDaily;
}

This is POJO of MetaData:

public class MetaData{
    public String _1Information;
    public String _2Symbol;
    public String _3LastRefreshed;
    public String _4OutputSize;
    public String _5TimeZone;
}

Pojo of TimeSeriesDaily:

public class TimeSeriesDaily {
    public _20200814 _20200814;
}

This trying to map Pojo using Java Jackson:

public static Map&lt;Alphavantage,Map&lt;TimeSeriesDaily,Object&gt;&gt; getAlphaData() {        
        RestAssured.baseURI = uri;
        RequestSpecification request = RestAssured.given().log().all();
        Response response = request.get(uri + &quot;/query?function=TIME_SERIES_DAILY&amp;symbol=IBM&amp;apikey=demo&quot;);
        String jsonResponse=response.getBody().asString();

        ObjectMapper mapper = new ObjectMapper();
        Map&lt;Alphavantage,Map&lt;TimeSeriesDaily,Object&gt;&gt; data = mapper.readValue(jsonResponse, new TypeReference&lt;Map&lt;Alphavantage,Map&lt;TimeSeriesDaily,Object&gt;&gt;&gt;(){});
        return data;
    }

答案1

得分: 2

为了使其正常工作,Java对象的名称必须与JSON字段键完全相同。

因此,Jackson将无法将 1. Information 映射到 _1Information
您需要使用类似这样的方式:

@JsonProperty("1. Information")
public String _1Information;

对于每个其他字段也是一样的。

英文:

In order for it to work, the Java object needs to have the exact same name as the JSON field key.

So, Jackson would not be able to map 1. Information to _1Information.
You need to use something like this:

@JsonProperty(&quot;1. Information&quot;)
public String _1Information;

The same holds for every other field.

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

发表评论

匿名网友

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

确定