Jackson error: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token

Jackson error: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token

嘿,我也有问题,这是我的 Json

[
{
    "aimid": "12345"
},
{
    "aimid": "333674"
},
{
    "aimid": [
        "4568999",
        "6789345"
    ]
}]

这是我的 Pojo class:-

@JsonProperty("aimid")
private String aimid;


public String getAimid() {
    return aimid;
}

public void setAimid(String aimid) {
    this.aimid = aimid;
}

我想将 aimid 存储在 pojo 中。当我在我的应用程序中像上面那样写时,我收到错误。

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token.

根据我的理解,我因为 Array 元素而出错,所以任何人都可以建议我如何捕获这两个东西,如果它是 String 或它是 Array String

挑战在于,在某些情况下 "aimid" 是一个字符串值,但在另一种情况下它是一个数组。

如果您可以控制 JSON 的结构,则更新结构,使根数组的每个元素都具有以下结构之一:

字符串 { "aimid":“333674” } 或阵列 { "aimid": [ "4568999", “6789345” ] }

如果您无法控制数据的结构,您将需要自己解析它并将其处理到您的 POJO 中。

请查看这 3 个代码示例,它们应该说明您可以如何使用此方法。 :

public class MyPojo {

    private List<String> aimid;

    @JsonProperty("aimid")
    public List<String> getAimid() {
    return aimid;
    }

    @JsonProperty("aimid_array")
    public void setAimid(final List<String> aimid) {
    this.aimid = aimid;
    }

    @JsonProperty("aimid")
    public void setAimid(final String aimid) {
    this.aimid = Arrays.asList(aimid);
    }
}


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.*;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.Test;

    public class UnitTest {

    private static final Logger LOGGER = Logger.getLogger(UnitTest.class.getName());

    public UnitTest() {
    }

    @Test
    public void testOneAimId() throws IOException {
    final String json = "[\n"
        + "{\n"
        + "    \"aimid\": \"12345\"\n"
        + "},\n"
        + "{\n"
        + "    \"aimid\": \"333674\"\n"
        + "}]";
    final List<MyPojo> result = new ObjectMapper().readValue(json, new TypeReference<List<MyPojo>>() {
    });
    log(Level.SEVERE, LOGGER, "testOneAimId", result);
    }

    @Test
    public void testListAimIds() throws IOException {
    final String json = "[\n"
        + "{\n"
        + "    \"aimid_array\": [\n" // HERE WE HAVE CHANGED THE JSON PROP NAME
        + "        \"4568999\",\n"
        + "        \"6789345\"\n"
        + "    ]\n"
        + "}]";
    final List<MyPojo> result = new ObjectMapper().readValue(json, new TypeReference<List<MyPojo>>() {
    });
    log(Level.SEVERE, LOGGER, "testListAimIds", result);
    }

    @Test
    public void testMixed() throws IOException {
    final String json = "[\n"
        + "{\n"
        + "    \"aimid\": \"12345\"\n"
        + "},\n"
        + "{\n"
        + "    \"aimid\": \"333674\"\n"
        + "},\n"
        + "{\n"
        + "    \"aimid_array\": [\n" // HERE WE HAVE CHANGED THE JSON PROP NAME
        + "        \"4568999\",\n"
        + "        \"6789345\"\n"
        + "    ]\n"
        + "}]";
    final List<MyPojo> result = new ObjectMapper().readValue(json, new TypeReference<List<MyPojo>>() {
    });
    log(Level.SEVERE, LOGGER, "testMixed", result);
    }

    @Test
    public void testMixed2() throws IOException {
    final String json = "[\n"
        + "{\n"
        + "    \"aimid\": \"12345\"\n"
        + "},\n"
        + "{\n"
        + "    \"aimid\": \"333674\"\n"
        + "},\n"
        + "{\n"
        + "    \"aimid\": [\n"
        + "        \"4568999\",\n"
        + "        \"6789345\"\n"
        + "    ]\n"
        + "}]";

    final JsonNode result = new ObjectMapper().readValue(json, JsonNode.class);
    final ArrayList<String> arrayList = new ArrayList<>();

    result.forEach((final JsonNode jsonNode) -> {

        if (jsonNode.getNodeType() != JsonNodeType.OBJECT)
        throw new IllegalArgumentException(jsonNode.toString());

        final ObjectNode obj = (ObjectNode) jsonNode;
        obj.forEach(o -> {
        switch (o.getNodeType()) {
            case ARRAY:
            final ArrayNode array = (ArrayNode) o;
            array.forEach(t -> arrayList.add(t.asText()));
            break;
            case STRING:
            arrayList.add(o.asText());
            break;
            default:
            throw new IllegalArgumentException(o.toString());
        }
        });
    });

    final MyPojo myPojo = new MyPojo();
    myPojo.setAimid(arrayList);
    log(Level.SEVERE, LOGGER, "myPojo", myPojo);
    }

    private void log(final Level level, final Logger logger, final String title, final Object obj) {
    try {
        if (title != null)
        logger.log(level, title);
        final ObjectWriter writer = new ObjectMapper().writerWithDefaultPrettyPrinter();
        logger.log(level, obj == null ? "null" : writer.writeValueAsString(obj));
    } catch (final JsonProcessingException ex) {
        logger.log(Level.SEVERE, ex.getMessage(), ex);
    }
    }
}