简体   繁体   English

JSON 使用 spring 中的 jackson 解析?

[英]JSON parsing using jackson in spring?

I have the following JSON我有以下 JSON

{
  "ads": [
    {
  "228029_228029": {
    "ad_id": "228029",
    "duration": 10,
    "m3u8_text": {
      "_1280p": "#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-ALLOW-CACHE:YES\n#EXT-X-TARGETDURATION:7\n#EXT-X-MEDIA-SEQUENCE:0\n#EXTINF:7.120000,\n_1280p_0000.ts\n#EXTINF:2.880000,\n_1280p_0001.ts\n#EXT-X-ENDLIST\n",
      "_320p": "#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-ALLOW-CACHE:YES\n#EXT-X-TARGETDURATION:7\n#EXT-X-MEDIA-SEQUENCE:0\n#EXTINF:7.120000,\n_320p_0000.ts\n#EXTINF:2.880000,\n_320p_0001.ts\n#EXT-X-ENDLIST\n"
    }
  }
},
{
  "228845_228845": {
    "ad_id": "228845",
    "duration": 24,
    "m3u8_text": {
      "_1280p": "#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-ALLOW-CACHE:YES\n#EXT-X-TARGETDURATION:8\n#EXT-X-MEDIA-SEQUENCE:0\n#EXTINF:7.840000,\n_1280p_0000.ts\n#EXTINF:6.880000,\n_1280p_0001.ts\n#EXTINF:6.680000,\n_1280p_0002.ts\n#EXTINF:2.600000,\n_1280p_0003.ts\n#EXT-X-ENDLIST\n",
      "_320p": "#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-ALLOW-CACHE:YES\n#EXT-X-TARGETDURATION:8\n#EXT-X-MEDIA-SEQUENCE:0\n#EXTINF:7.840000,\n_320p_0000.ts\n#EXTINF:6.880000,\n_320p_0001.ts\n#EXTINF:6.680000,\n_320p_0002.ts\n#EXTINF:2.600000,\n_320p_0003.ts\n#EXT-X-ENDLIST\n"
    }
  }
}
  ],
  "total_duration": 80
}

I have created the respective model class as This is the root model我已经创建了相应的 model class 因为这是根 model

@JsonIgnoreProperties(ignoreUnknown = true)
public class AdsResponse {
@JsonProperty("ads")
List<Ad> ads;
@JsonProperty("total_duration")
long totalDuration;
}

Then the ads model然后是广告 model

public class Ad {
Map<String,AdInfo> ad;}

Then the AdInfo model然后 AdInfo model

public class AdInfo {
@JsonProperty("m3u8_text")
AdManifest adManifest;
int duration;
@JsonProperty("ad_id")
String adId;}

Then the manifest model然后清单 model

public class AdManifest {
@JsonProperty("_1280p")
String _1280p;
@JsonProperty("_320p")
String _320p;}

When I try to parse this using below code当我尝试使用下面的代码解析它时

AdsResponse response = new ObjectMapper().readValue(
                res,
                AdsResponse.class);

I get the empy ad object我收到了 empy 广告 object

AdsResponse{ads=[Ad{ad=null}, Ad{ad=null}, totalDuration=80}

What is wrong here?这里有什么问题?

You don't actually need the Ad class, it's just a Map<> .您实际上并不需要Ad class,它只是一个Map<> AdResponse can look like this: AdResponse可能如下所示:

public class AdsResponse {

    @JsonProperty("ads")
    List<Map<String, AdInfo>> ads;

    @JsonProperty("total_duration")
    long totalDuration;

If the keys in the map were predictable, you could make them properties on the Ad class and then Jackson would map them properly.如果 map 中的键是可预测的,您可以在Ad class 上设置它们的属性,然后 Jackson 将它们正确地设置为 Z1AE8BDC8ED51FE21449E。 But since they're not (they look like some kind of ID), mapping them to a Map<> is probably the best option.但由于它们不是(它们看起来像某种 ID),将它们映射到Map<>可能是最好的选择。

As an alternative, if you want or need to have the Ad objects, you can map them like this:作为替代方案,如果您想要或需要Ad对象,您可以像这样 map 它们:

public class Ad {

    Map<String, AdInfo> adInfo = new HashMap<>();

    @JsonAnySetter
    public void setAds(String key, AdInfo value) {
        adInfo.put(key, value);
    }
}

With that, and AdResponse defined the way you have it in the question , you'll get populated Ad instances, each of which has a Map<> with only 1 key/value pair.有了它,并且AdResponse定义了您在问题中的方式,您将获得填充的Ad实例,每个实例都有一个Map<> ,只有 1 个键/值对。 For an even simpler (and probably more sensible model, you can eliminate the Map if there is only ever 1 key in an Ad , like this:对于更简单(并且可能更明智的 model),如果Ad中只有一个键,则可以消除Map ,如下所示:

public class Ad {

    private AdInfo adInfo;

    @JsonAnySetter
    public void setAdInfo(String ignored, AdInfo value) {
        this.adInfo = value;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM