0

Good Morning,

I'm relatively new to java and trying to to work through a project, but I'm running into issues. My program uses SQL to build a URL to call a web-service, this service will return the following JSON:

[{
    "MmisItemNo": "106552",
    "CatalogNo": "UM18840041R",
    "ContractOn": "False"
 }
]

What I need to do is strip off the "False" or "True" value at the end to do additional logic depending on its result.

I keep getting the following error:

.WebCall -com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot 
deserialize instance of `.LightswitchResponse` out of START_ARRAY token at [Source: (String)"[{"MmisItemNo":"106552","CatalogNo":"UM18840041R","ContractOn":"False"}]"; line: 1, column: 1]

Here is my code currently after the URL is built in the WebCall.java class:

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
    }
    in.close();
    System.out.println(response.toString());
ObjectMapper mapper = new ObjectMapper();
LightswitchResponse lightswitchResponse = mapper.readValue(response.toString(), LightswitchResponse.class);
System.out.println(lightswitchResponse.ContractOn);

In my second class the code is as follows:

public class LightswitchResponse {

String MmisItemNo;
String CatalogNo;
boolean ContractOn;

public String getMmisItemNo() {
    return MmisItemNo;
}
public void setMmisItemNo(String mmisItemNo) {
    MmisItemNo = mmisItemNo;
}
public String getCatalogNo() {
    return CatalogNo;
}
public void setCatalogNo(String catalogNo) {
    CatalogNo = catalogNo;
}
public boolean ContractOn() {
    return ContractOn;
}
public void setContractOn(boolean contractOn) {
    ContractOn = contractOn;
}

}

Any ideas on what might be going wrong? I feel like it might have something to do with the [] on the JSON response, but not 100% sure.

2
  • I took your error message, stripped all the custom stuff, and searched for it on google. That duplicate was the first result. Please get into the habit of doing that before posting questions. See How much research effort is expect of StackOverflow users?. Commented Feb 8, 2018 at 15:54
  • Sorry about that, I didn't know if my situation was different since I was using Jackson. I'll do more research before my next post, thanks! Commented Feb 8, 2018 at 16:09

1 Answer 1

1

You are right it is because of the [] on the JSON response. This means that the JSON object that is being returned is an array of objects.

Hence to get Jackson to map it correctly you should be doing this:

LightswitchResponse[] lightswitchResponses = 
      mapper.readValue(response.toString(), LightswitchResponse[].class);

You can then read the first element off this to get your desired LightSwitchResponse object:

LightswitchResponse lightswitchResponse = lightswitchResponses[0];
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the response, when I try to use that additional line I get this error: the type of the expression must be an array type but it resolved to LightswitchResponse
I missed an s on the second line I fixed it now.
Thanks, now the error changed into: Unrecognized field "MmisItemNo" (c.LightswitchResponse), not marked as ignorable (3 known properties: "contractOn", "catalogNo", "mmisItemNo"]) at [Source: (String)"[{"MmisItemNo":"106552","CatalogNo":"UM18840041R","ContractOn":"False"}]"; line: 1, column: 17] (through reference chain: java.lang.Object[][0]
added this above your first code: mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
It is now working, thank you!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.