简体   繁体   中英

Parsing JSON with jackson, spring?

I have the following 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
      }
    ]
  }
}

Have created the model class as follows

This is the root

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

The playlist model which has m3u8_ob and scte35

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

The scte35 model

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

The m3u8_ob model

@JsonIgnoreProperties(ignoreUnknown = true)
public class M3U8 {

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

When I pass the JSON string to jackson I get null

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

What is wrong with the model class here? Root object key could be random that is why I have taken it as a map. Assume the setters and getters already there in the model classes.

I suppose it will try to map to a field playlists because that's the name of your map field in the Playlists class, but the root object doesn't have that at all. I think you should immediately map to a Map<String, Playlist>

Also the key in your json is m3u8_ob but your field is called m3u8, why? That seems like a likely candidate for future problems. Other fields don't match either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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