Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.0k views
in Technique[技术] by (71.8m points)

json - com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token

How to read below JSON using Jackson ObjectMapper? I have developed code but getting below error.

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
 at [Source: (File); line: 7, column: 19] (through reference chain: com.example.demo.resources.Orgnization["secondaryIds"])

JSON

{
  "id": "100000",
  "name": "ABC",
  "keyAccount": false,
  "phone": "1111111",
  "phoneExtn": "11",
  "secondaryIds": {
    "ROP": [
      "700010015",
      "454546767",
      "747485968",
      "343434343"
    ],
    "AHID": [
      "01122006",
      "03112001"
    ]
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to enable ACCEPT_SINGLE_VALUE_AS_ARRAY feature. Probably in POJO you have a List but when there is only one element in a List JSON payload is generated without array brackets.

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.util.List;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./src/main/resources/test.json");

        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);

        Orgnization root = mapper.readValue(jsonFile, Orgnization.class);
        System.out.println(root);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...