In my java spring project, I have string of json array with objects.I wan to fetch data from string and then save to the database.
So,I have: pojo class
public class Contact {
private String id;
private String name;
private String mobile;
//getters and settrs & constructors
}
and I want to fetch data like below code:
String stringdata=["{"id":"1","mobile":"1860108","name":"Intex"}",
"{"id":"21","mobile":"777717717","name":"pari"}",
"{"id":"26","mobile":"172676236","name":"pari2"}"];
try {
Contact contact1 = new Contact();
try {
contact1 = new Gson().fromJson(contactreceive, Contact.class);
}
catch (UnsupportedEncodingException uee) {
return new ResponseEntity<Object>("failed", HttpStatus.EXPECTATION_FAILED);
}
Contact contact = new Contact(contact1.getId(),contact1.getName(),contact1.getMobile());
userDao.saveContact(contact);
return new ResponseEntity<Object>("created", HttpStatus.CREATED);
} catch (Exception e) {
logger.error("Mobile User Signup > Error: " + e.getMessage());
return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
}
}
But I am getting error:
Mobile User Signup > Error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
without remove array,How can I fetch that contacts?