简体   繁体   中英

JSON de-serialization using jackson

I've a JSON which I want to deserialize to java object. I tried but failed to succeed. Really appreciate if somebody help it. I was getting below error.

ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
// Note : vairable 'body' is the JSON string which I've shared below.
RpcResponse rs = mapper.readValue(body, RpcResponse.class);

Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of Result out of START_ARRAY token

{
"error": null,
"id": "12345",
"result": {
    "inventory": [{
        "history": [{
            "when": "2012-08-30T07:28:51Z",
            "changes": [{
                "removed": "",
                "added": "1",
                "field_name": "qty"
            },
            {
                "removed": "normal",
                "added": "major",
                "field_name": "popularity"
            }],
        "id": 474599,
        "alias": null
    }]
}

}

Here are the java classes

public class RpcResponse {

private String error;
private String id;
private Map<String, Result> result;

public String getError() {
    return error;
}

public void setError(String error) {
    this.error = error;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public Result getResult() {
    return result;
}

public void setResult(Result result) {
    this.result = result;
}

}

public class Result {

private Map<String, List<Inventory>> inventory;

public Map<String, List<Inventory>> getBugs() {
    return inventory;
}

public void setBugs(Map<String, List<Inventory>> inventory) {
    this.inventory = inventory;
}

}

public class Inventory {

private String id;
private String alias;
private Map<String, List<History>> history;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getAlias() {
    return alias;
}

public void setAlias(String alias) {
    this.alias = alias;
}

public Map<String, List<History>> getHistory() {
    return history;
}

public void setHistory(Map<String, List<History>> history) {
    this.history = history;
}

}

public class History {

private String who;
private String when;
private Map<String, Changes> changes;

public String getWho() {
    return who;
}
public void setWho(String who) {
    this.who = who;
}
public String getWhen() {
    return when;
}
public void setWhen(String when) {
    this.when = when;
}
public Map<String, Changes> getChanges() {
    return changes;
}
public void setChanges(Map<String, Changes> changes) {
    this.changes = changes;
}

}

In RCP Response,

private Map<String, Result> result;

should just be

private Result result;

In Result,

private Map<String, List<Inventory>> inventory;

should be

private List<Inventory> inventory;

and in Inventory,

private Map<String, List<History>> history;

should be

private List<History> history;

In History, Map<String,Changes> should be a Collection<Changes> , etc

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