简体   繁体   中英

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

UnrecognizedPropertyException is because Message is not a recognized property.

Without annotations, the default name of the fields start with lowercase, eg 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 .

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