Reputation: 1405
I try to read from controller input json
When I uses name in node, everything is okey
There is json
{
"itemList": [
{
"name": "Alex",
"surname": "Ivanov",
"age": "25"
},
{
"name": "Daria",
"surname": "Ivanova",
"age": "23"
}
]
}
there is itemList in root of json
And I can catch it by this classes
controller
@RequestMapping(value = "/users",
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UserController {
@Post
public ResponseEntity add(@RequestBody UserDto user) {
//todo check breack point hear
return new ResponseEntity<UserDto>(user, null, HttpStatus.OK);
}
}
and model
@RequiredArgsConstructor
@Getter
@Setter
@ToString
@EqualsAndHashCode
public class UserDto implements Serializable {
public List<UserItem> itemList;
}
@RequiredArgsConstructor
@Getter
@Setter
@ToString
@EqualsAndHashCode
public class UserItem implements Serializable {
private String name;
private String surname;
private String age;
}
But, I need in really, I need to parse json like this:
Just items in objects without name
{
[
{
"name": "Alex",
"surname": "Ivanov",
"age": "25"
},
{
"name": "Daria",
"surname": "Ivanova",
"age": "23"
}
]
}
How to make it?
Upvotes: 0
Views: 2027
Reputation: 2385
This is a malformed JSON object. The array inside doesn't have any key.
{
[
{
"name": "Alex",
"surname": "Ivanov",
"age": "25"
},
{
"name": "Daria",
"surname": "Ivanova",
"age": "23"
}
]
}
I think what you're looking for is a JSON array:
[
{
"name": "Alex",
"surname": "Ivanov",
"age": "25"
},
{
"name": "Daria",
"surname": "Ivanova",
"age": "23"
}
]
To parse this JSON array, just modify your Controller to accept a list of UserItem
:
@RequestBody List<UserItem> users
Upvotes: 1