I know this question or similar ones have been asked multiple times here, but I swear I've read tons of related posts and none of the solutions seems to be working for me.
I'm trying to map some JSON data, which I receive from a specific REST API, to a Java class. The JSON has this format:
{"netStatLinks":[
{"src":"of:0000000000000002/3",
"dst":"of:0000000000000000/1",
"bw":10000.0,
"rate":0.0,
"usage":0.0,
"available":10000.0},
{"src":"of:0000000000000005/3",
"dst":"of:0000000000000006/1",
"bw":10000.0,
"rate":0.0,
"usage":0.0,
"available":10000.0},
{...and so on}
]}
The Java class I want to map the JSON to is this:
public class NetStatLinkList {
private List<NetStatLink> netStatLinks;
// Generated getter and setter
@Override
public String toString(){
return netStatLinks.toString();
}
}
and
public class NetStatLink {
public String src;
public String dst;
public double bw;
public double rate;
public double usage;
public double available;
public NetStatLink(String src, String dst, double bw, double rate, double usage, double available){
this.src = src;
this.dst = dst;
this.bw = bw;
this.rate = rate;
this.usage = usage;
this.available = available;
}
// Generated getters and setters
@Override
public String toString() {
return "Link "+src +"to"+ dst+" -- "+"rate: "+rate;
}
}
I can read the JSON correctly but I can't manage to map it in any way. I've tried all this options without success (note that url is an URL object created based on the url string and that all the neccessary exceptions are implicitly catched):
Using Jackson to directly map the JSON to NetStatLinkList
ObjectMapper mapper = new ObjectMapper();
NetStatLinkList linkLst = mapper.readValue(url,NetStatLinkList.class);
Using Jackson to map the JSON to a List of NetStatLink objects
TypeReference<List<NetStatLink>> type = new TypeReference<List<NetStatLink>>() {};
List<NetStatLink> linkLst = mapper.readValue(url, mapType);
Using Jackson to map the JSON string directly to the class/to a List of objects
JsonNode rootNode = mapper.readValue(url,JsonNode.class);
JsonNode data = rootNode.get("netStatLinks");
List<NetStatLink> linkLst = mapper.readValue(data.toString(), mapType);
// or
NetStatLink[] linkLst = mapper.readValue(data.toString(), NetStatLink[].class)
// or
List<NetStatLink> linkLst = mapper.readValue(data.traverse(), mapType);
Using Jackson to map the JSON to an ArrayNode and iterate through it
JsonNode linkData = mapper.readTree(url);
ArrayNode linkDataArray = (ArrayNode)linkData.get("netStatLinks");
List<NetStatLink> linkLst;
while(linkDataArray.elements().hasNext()){
linkLst.add(mapper.readValue(linkDataArray.elements().next().toString(),NetStatLink.class));
}
Using the external library json-simple to read the JSON and then map it with Jackson
String location = IOUtils.toString(url);
JSONObject jo = (JSONObject) JSONValue.parseWithException(location);
JSONArray val = (JSONArray) jo.get("NetStatLinks");
List<NetStatLink> linkLst = mapper.readValue(val.toJSONString(), mapType);
// or
NetStatLink[] linksList = mapper.readValue(val.toJSONString(), NetStatLink[].class);
Do you know if there's any other way I could map this JSON data to my classes, or if there's any mistake in those methods which is the reason I cannot get them to work?