3

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?

Sizigia
  • 582
  • 5
  • 10

1 Answers1

2

You can use gson

    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.1</version>
    </dependency>

JsonParser jsonParser = new JsonParser();
            JsonObject jo = (JsonObject)jsonParser.parse(str);
            JsonArray jsonArr = jo.getAsJsonArray("netStatLinks");

    List<NetStatLink> users = new ArrayList<NetStatLink>();
       Gson gson = new Gson();
       Type listType = new TypeToken<List<NetStatLink>>(){}.getType();
       netStatLink = gson.fromJson(jsonArr ,listType);
gati sahu
  • 2,576
  • 2
  • 10
  • 16
  • I'll try that, but just one question: what is the variable 's' exactly? I mean, is it the JSON formatted as received, the value of "netStatLinks" taken from the JSON data, the JSON converted into a string... ? – Sizigia Jun 06 '17 at 13:15
  • I tried it with the JSON string from the ArrayNode and it works! Thanks so, so much, really! (I tried it before seeing your answer update, but thanks anyway for the JsonParser option!) – Sizigia Jun 06 '17 at 13:33