【问题标题】:Parsing JSON with jackson, spring?用杰克逊,春天解析JSON?
【发布时间】:2022-07-21 21:03:00
【问题描述】:

我有以下 JSON

{
  "iD19.m3u8": {
    "m3u8_ob": {
      "type": "playlist",
      "version": 3
    }
  },
  "iD19180_400.m3u8": {
    "m3u8_ob": {
      "type": "playlist",
      "version": 3
    },
    "scte35": [
      {
        "start": 9,
        "end": null,
        "duration": 62
      }
    ]
  },
  "iD19180_700.m3u8": {
    "m3u8_ob": {
      "type": "playlist",
      "version": 3
    },
    "scte35": [
      {
        "start": 9,
        "end": null,
        "duration": 62
      }
    ]
  }
}

已创建模型类如下

这是根

@JsonIgnoreProperties(ignoreUnknown = true)
public class Playlists {
Map<String,Playlist> playlists; 
}

具有m3u8_obscte35的播放列表模型

@JsonIgnoreProperties(ignoreUnknown = true)
public class Playlist {
SCTE35 scte35;
M3U8 m3u8;
}

scte35 模型

@JsonIgnoreProperties(ignoreUnknown = true)
public class SCTE35 {
Object start;
Object end;
long duration;
String value;
}

m3u8_ob 模型

@JsonIgnoreProperties(ignoreUnknown = true)
public class M3U8 {

boolean isMasterPlaylist;
int version;
boolean independentSegments;
String source;
long targetDuration;
}

当我将 JSON 字符串传递给 jackson 时,我得到 null

Playlists sessionData = objectMapper.readValue(new String(data.value()), Playlists.class);
                System.out.println(sessionData.toString());

这里的模型类有什么问题?根对象键可能是随机的,这就是我将其作为地图的原因。假设模型类中已经存在 setter 和 getter。

【问题讨论】:

    标签: java json spring jackson


    【解决方案1】:

    我想它会尝试映射到 playlists 字段,因为这是 Playlists 类中映射字段的名称,但根对象根本没有。我认为您应该立即映射到Map&lt;String, Playlist&gt;

    您的 json 中的键也是 m3u8_ob 但您的字段称为 m3u8,为什么?这似乎是未来问题的可能候选者。其他字段也不匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      相关资源
      最近更新 更多