Tag Archives: out of start_ ARRAY token error

Exception information: jsonmappingexception: out of start_ ARRAY token

JsonMappingException: out of START_ARRAY token
How to Fix this error
Json

[
  {
    "id": 4,
    "dmNum": "111102",
    "number": "683272",
    "parentNum": "0",
    "type": "1",
    "name": "dashen",
    "code": "213134",
    "mDefault": "37",
    "description": "please ask",
    "isDel": "0",
    "opFlag": "A",
    "createdBy": "creator",
    "createdTime": "2020-10-15T05:20:17.000+0000",
    "updatedBy": "deleter",
    "updatedTime": "2020-10-15T05:28:18.000+0000",
    "children": null,
    "parentName": null
  },
  {
    "id": 5,
    "dmNum": "111102",
    "number": "68327201",
    "parentNum": "683272",
    "type": "0",
    "name": "temperature data",
    "code": "213135",
    "mDefault": "95",
    "description": "I dont know",
    "isDel": "0",
    "opFlag": "A",
    "createdBy": "creator",
    "createdTime": "2020-10-15T05:20:18.000+0000",
    "updatedBy": "deleter",
    "updatedTime": "2020-10-15T05:27:33.000+0000",
    "children": null,
    "parentName": null
  }
]

Create ObjectMapper

    public static ObjectMapper mapper = new ObjectMapper();

    static {
        // Convert to formatted json
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        // If there are new fields in the json that do not exist in the entity class, no error will be reported
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // Modify date format
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

Java code:

String s1 = httpAPIService
                    .doGet("http://192.168.0.25:8888/modeloutput/selectOutputByModelId?mid=" + mid);
            ModelOutput[] modelOutput =  mapper.readValue(s1, ModelOutput[].class);//Output Table Object
            for (ModelOutput output : modelOutput) {
                //Assignment: metadata number
                deviceOutput.setMetaNum(output.getNumber());
                //Assignment; data encoding
                deviceOutput.setCode(output.getOutputCode());
                deviceOutputMapper.save(deviceOutput);
                System.out.println(deviceOutput);
            }

The outermost layer of this string of JSON data is [], which represents an array of objects, because the Jackson Object Mapper is converting the returned JSON fragment into an object.
Convert to an array object

ModelOutput[] modelOutput =  mapper.readValue(s1, ModelOutput[].class);

Iterate through the data in the array. Convert to a Java object.
idea shortcut key: iter

for (ModelOutput output : modelOutput) {
                //Assignment: metadata number
                deviceOutput.setMetaNum(output.getNumber());
                //Assignment; data encoding
                deviceOutput.setCode(output.getOutputCode());
                deviceOutputMapper.save(deviceOutput);
                System.out.println(deviceOutput);
            }

Done!