Search code examples
javajsonstringobjectmapperjsonparser

Map json to object with multiple "" in java


i'm trying to use object mapper to convert a json string to an object class, but it keep return UnrecognizedPropertyException

here is the return json string

{"errorCode":400,"Message":"ORA-01403: no data found\nORA-06512: at \"CSS_HPG.SELFCARE_LAY_DS_GOITRATRUOC\", line 12\nORA-06512: at line 1","Data":null}

here is my class

import java.util.ArrayList;
import java.util.List;

/**
*
* @author Autumn
*/
public class PackageSearchOutput {

int errorCode;
List<String> Message;
PackageSearchOutputData Data;

public PackageSearchOutput(int errorCode, List<String> Message, 
PackageSearchOutputData Data) {
    this.errorCode = errorCode;
    this.Message = Message;
    this.Data = Data;
}


public PackageSearchOutput() {
}

public List<String> getMessage() {
    return Message;
}

public void setMessage(ArrayList<String> Message) {
    this.Message = Message;
}

public int getErrorCode() {
    return errorCode;
}

public void setErrorCode(int errorCode) {
    this.errorCode = errorCode;
}



public PackageSearchOutputData getData() {
    return Data;
}

public void setData(PackageSearchOutputData Data) {
    this.Data = Data;
}

}

here is my mapping code

result = mapper.readValue(output.toString(), PackageSearchOutput.class);

here is the detail error click to see


Solution

  • UnrecognizedPropertyException is because Message is not a recognized property.

    Without annotations, the default name of the fields start with lowercase, e.g. message, but your JSON text has Message, which is a different name (since they are case-sensitive).

    To use non-default property names, add @JsonProperty annotations:

    @JsonProperty("Message")
    public List<String> getMessage() {
    
    @JsonProperty("Data")
    public PackageSearchOutputData getData() {
    

    Once you do that, you're likely going to run into the issue of Message being a String, while your class defines it as a List<String>, so that may fail. You might need to change the Message field and its getter and setter from List<String> to String.