简体   繁体   中英

Converting JSON response to Java object

I need to convert a JSON response into Java Object, but I am gettin nullpointerException. Here is my model class:

public class Cheque {

private String payeeName;
private String accountNumber;
private String ifsCode;
private String micr;
private String bankName;

public Cheque() {
    super();
    // TODO Auto-generated constructor stub
}

public Cheque(String payeeName, String accountNumber, String ifsCode, String micr, String bankName) {
    super();
    this.payeeName = payeeName;
    this.accountNumber = accountNumber;
    this.ifsCode = ifsCode;
    this.micr = micr;
    this.bankName = bankName;
}


public String getPayeeName() {
    return payeeName;
}

public void setPayeeName(String payeeName) {
    this.payeeName = payeeName;
}

public String getAccountNumber() {
    return accountNumber;
}

public void setAccountNumber(String accountNumber) {
    this.accountNumber = accountNumber;
}

public String getIfsCode() {
    return ifsCode;
}

public void setIfscCode(String ifsCode) {
    this.ifsCode = ifsCode;
}

public String getMicr() {
    return micr;
}

public void setMicr(String micr) {
    this.micr = micr;
}

public String getBankName() {
    return bankName;
}

public void setBankName(String bankName) {
    this.bankName = bankName;
}

Below I am posting the method in which I am invoking a Python program and getting a Json response from it:

public class RunPython {

public static void main(String[] args) throws IOException,ScriptException,NullPointerException{
    // TODO Auto-generated method stub
    Process p = Runtime.getRuntime().exec("python <path to file>/reg.py");
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line ;
    try {
        while((line =in.readLine()) != null) {
            System.out.println(line);
        }
   } finally {
     in.close();
   }

    ObjectMapper mapper = new ObjectMapper();
    try { 

        Cheque cheque = mapper.readValue(line, Cheque.class); 
        System.out.println("Java object :"); 
        System.out.println(cheque); 
        }
    catch (JsonGenerationException ex) { 
        ex.printStackTrace(); 
        } 
    catch (JsonMappingException ex) {
        ex.printStackTrace(); 
        } 
    catch (IOException ex) { 
        ex.printStackTrace(); 
        }}}

The JSON response which I am getting is:

{
"bankName": [
    "Bank"
],
"accountNumber": [
    "989898989898"
],
"ifsCode": [
    "0000000"
],
"micr": [
    "0000000"
],
"payeeName": [
    "name"
]

}

After running the program I am getting the JSON response as expected, but while converting it to a Java object it is showing nullPointerException in the main thread. Help me out to find where I am making the mistake.

You consume/exhaust all your Process Inputstream here when printing it :

 try {
        while((line =in.readLine()) != null) {
            System.out.println(line);
        }

And later on when you call the below it's already null :

Cheque cheque = mapper.readValue(line, Cheque.class); 

You'd have to process it in the above while loop, instead of only printing it out

When you reach the statement:

    Cheque cheque = mapper.readValue(line, Cheque.class); 

variable line is null.

So you can either rebuild the JSON string (with a StringBuilder), or remove the code that prints the response, and parse the JSON directly from p.getInputStream() .

您的Java对象具有String值,但是JSON具有数组类型([])

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