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.

David Brossard
  • 13,584
  • 6
  • 55
  • 88
jbartel86
  • 9
  • 1
  • 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?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). – Sotirios Delimanolis Feb 08 '18 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! – jbartel86 Feb 08 '18 at 16:09

1 Answers1

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];
Plog
  • 9,164
  • 5
  • 41
  • 66
  • 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 – jbartel86 Feb 08 '18 at 16:17
  • I missed an s on the second line I fixed it now. – Plog Feb 08 '18 at 16:20
  • 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] – jbartel86 Feb 08 '18 at 16:56
  • added this above your first code: mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); – jbartel86 Feb 08 '18 at 20:56
  • It is now working, thank you! – jbartel86 Feb 08 '18 at 20:56
  • For some reason now, its just saying false for everyone regardless if the string says True or False, any ideas? – jbartel86 Feb 14 '18 at 21:34
  • Possibly doesn't like the capital letters in True/False should be true/false – Plog Feb 15 '18 at 08:59