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!
Read More:
- [Solved] JSON parse error: Cannot deserialize instance of `java.util.ArrayList<..> out of START_OBJECT token;
- Java uses class array to report error Exception in thread “main” java.lang.NullPointerException solution
- JAVA Code Start Exception Error View Does Not Exsit [Solved]
- [Solved] swagger Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullP
- [Solved] Failed to start bean ‘eurekaAutoServiceRegistration‘; nested exception is java.lang.NullPointerExce
- [Solved] Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
- [Solved] Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPointerEx
- [Solved] Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerExcepti
- [Solved] eureka Startup Error: Unable to start web … nested exception is org.springframework.boot.web.server.WebS
- [Solved] eureka Startup Error: Unable to start web … nested exception is org.springframework.boot.web.server.WebS
- Java: How to Find the Minimum Value from a Random Array by Bubble Sort Method
- [Solved] ElasticsearchStatusException[Elasticsearch exception [type=mapper_parsing_exception, reason=failed t
- [Solved] IO exception: NL exception was generated
- C language: Implementation of dynamic array initialization, insertion, traversal, deletion and destruction
- JAVE: LeetCode(189) Rotate Array
- Uncaught SyntaxError: Unexpected token o in JSON at position 1 [How to Solve]
- [Solved] org.hibernate.exception.SQLGrammarException: could not extract ResultSet at org.hibernate.exception
- [Solved] Tomcat7 Start Error: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardCon
- [Solved] ERROR: child process failed, exited with error number 100To see additional information in this …
- [Modified] ANTLR4 line 1:14 token recognition error at ‘(‘