I am new to Json Parsing issue I have the below Json object and I need to get the User object from the below Json
{
"aud": "RoomyClinetApps",
"sub": "AAAA",
"User": {
"firtsName": "Godavarthi",
"LastName": "chaitanya"
},
"iss": "Roomy",
"iat": 1499279510
}
The below is the code I am trying to pull the User object where the the above json is in jsonInString variable when i trying t print I am getting null
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
User user= mapper.readValue(jsonInString.toString(), User.class);
System.out.println(user.getFirtsName()); --> Null
public class User {
#JsonProperty
private String firtsName;
#JsonProperty
private String LastName;
}
Could some one please Provide me a working code which is more appreciable.
Thanks
Chaitanya
Tested the below code. It worked for me. Try this once.
public class Test {
public static void main(String[] args) throws IOException {
String jsonInString = "{\n"
+ " \"aud\": \"RoomyClinetApps\",\n"
+ " \"sub\": \"AAAA\",\n"
+ " \"user\": {\n"
+ " \"firtsName\": \"Godavarthi\",\n"
+ " \"LastName\": \"chaitanya\"\n"
+ " },\n"
+ " \"iss\": \"Roomy\",\n"
+ " \"iat\": 1499279510\n"
+ "}";
ObjectMapper mapper = new ObjectMapper().configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonObject jobj = mapper.readValue(jsonInString.toString(), JsonObject.class);
System.out.println(jobj.getUser().getFirtsName());
}
}
class User {
private String firtsName;
private String LastName;
public String getFirtsName() {
return firtsName;
}
public void setFirtsName(String firtsName) {
this.firtsName = firtsName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String LastName) {
this.LastName = LastName;
}
}
class JsonObject {
private String aud;
private String sub;
private User user;
private String iss;
private long iat;
public String getAud() {
return aud;
}
public void setAud(String aud) {
this.aud = aud;
}
public String getSub() {
return sub;
}
public void setSub(String sub) {
this.sub = sub;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getIss() {
return iss;
}
public void setIss(String iss) {
this.iss = iss;
}
public long getIat() {
return iat;
}
public void setIat(long iat) {
this.iat = iat;
}
}
Hope this is helpful.
public class User {
#JsonProperty
private String firtsName;
#JsonProperty
private String LastName;
}
public class JsonObject{
public string aud;
public String sub;
public User user;
public String iss;
public long iat;
}
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonObject user= mapper.readValue(jsonInString.toString(), JsonObject.class);
Related
I have a list of objects instantiated in a return class in a service's response. But when this list has 1 position, it is returning as an object and not as a list. can anybody help me?
#XmlRootElement(name = "exame")
public class ExameVO implements Serializable {
private static final long serialVersionUID = -5840337332057658386L;
private long id;
#NotNull
private String cpf;
#NotNull
private String dataNascimento;
#NotNull
private int tipoExame;
#NotNull
private String excluido;
private List<ExameArquivoVO> resultadoExameArquivos;
private String observacao;
private String dataInclusao;
public ExameVO() {
super();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getDataNascimento() {
return dataNascimento;
}
public void setDataNascimento(String dataNascimento) {
this.dataNascimento = dataNascimento;
}
public int getTipoExame() {
return tipoExame;
}
public void setTipoExame(int tipoExame) {
this.tipoExame = tipoExame;
}
public String getObservacao() {
return observacao;
}
public void setObservacao(String observacao) {
this.observacao = observacao;
}
public String getExcluido() {
return excluido;
}
public void setExcluido(String excluido) {
this.excluido = excluido;
}
public List<ExameArquivoVO> getResultadoExameArquivos() {
return resultadoExameArquivos;
}
public void setResultadoExameArquivos(List<ExameArquivoVO> resultadoExameArquivos) {
this.resultadoExameArquivos = resultadoExameArquivos;
}
public String getDataInclusao() {
return dataInclusao;
}
public void setDataInclusao(String dataInclusao) {
this.dataInclusao = dataInclusao;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
#Override
public String toString() {
return "ResultadoExameVO [id=" + id + ", cpf=" + cpf + ", dataNascimento=" + dataNascimento + ", tipoExame="
+ tipoExame + ", observacao=" + observacao + ", excluido=" + excluido + ", dataInclusao=" + dataInclusao
+ ", resultadoExameArquivos=" + resultadoExameArquivos + "]";
}
}
#Override
public List<ExameVO> listarExames(String cpf, String dtNascimento, Long tipoExame) throws WebServiceException {
List<ExameVO> examesVO = new ArrayList<ExameVO>();
try {
examesVO = exameDAO.listarExames(cpf, dtNascimento, tipoExame);
if(!examesVO.isEmpty()) {
for(ExameVO exameVO : examesVO) {
exameVO.setResultadoExameArquivos(new ArrayList<ExameArquivoVO>());
exameVO.getResultadoExameArquivos().addAll(exameDAO.listarExameArquivos(exameVO.getId()));
}
}
return examesVO;
} catch (Exception e) {
throw new WebServiceException(e);
}
}
Response to postman: Notice that there is 1 object that has 2 objects inside the file list and another object that has 1 and it returns as an object and not as a list with 1 object.
Hi in the below code modules list is not coming after parsing the json and i have created different pojo classes for different classes and i am not getting the expected json response from server.
Can any one tell me where i did the mistake for json parsing and complete json is not coming as json
Activity.java:
final String username = username1.getText().toString();
final String password = password1.getText().toString();
String operation = "loginAndFetchModules";
final GetNoticeDataService service = RetrofitInstance.getRetrofitInstance().create(GetNoticeDataService.class);
/** Call the method with parameter in the interface to get the notice data*/
Call<LoginAndFetchModules> call1 = service.GetLoginModuleList(operation, username, password);
/**Log the URL called*/
Log.wtf("URL Called", call1.request().url() + "");
call1.enqueue(new Callback<LoginAndFetchModules>() {
#Override
public void onResponse(Call<LoginAndFetchModules> call1, Response<LoginAndFetchModules> response) {
Log.e("response",new Gson().toJson(response.body()));
if (response.isSuccessful()) {
Log.e("response",new Gson().toJson(response.body()));
LoginAndFetchModules loginAndFetchModules = response.body();
String success = loginAndFetchModules.getSuccess();
if (success.equals("true")) {
ArrayList<String> modules = new ArrayList<String>();
try {
JSONArray jsonArray = new JSONArray(loginAndFetchModules);
for (int i = 0; i < jsonArray.length(); i++) {
modules.add(jsonArray.get(i).toString());
JSONObject jsonObject=new JSONObject();
String id=jsonObject.getString("id").toString();
Log.i("id", ":" + id);
String name=jsonObject.getString("name").toString();
Log.i("name", ":" + name);
String isEntity=jsonObject.getString("isEntity").toString();
String label=jsonObject.getString("label").toString();
Log.i("isEntity", ":" + isEntity);
String singular=jsonObject.getString("singular").toString();
Log.i("singular", ":" + singular);
}//end for
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
LoginAndFetchModules.java:
public class LoginAndFetchModules {
#SerializedName("success")
private String success;
#SerializedName("result")
private List<Results> result;
public List<Results> getResult() {
return result;
}
public void setResult(List<Results> result) {
this.result = result;
}
public String getSuccess() {
return success;
}
public void setSuccess(String success) {
this.success = success;
}
}
Results.java:
public class Results {
#SerializedName("login")
#Expose
private GetLoginListDetails login;
#SerializedName("modules")
#Expose
private ArrayList<LoginListForModules> modules;
public ArrayList<LoginListForModules> getModules() {
return modules;
}
public void setModules(ArrayList<LoginListForModules> modules) {
this.modules = modules;
}
public GetLoginListDetails getLogin() {
return login;
}
public void setLogin(GetLoginListDetails login) {
this.login = login;
}
}
LoginListForModules.java:
public class LoginListForModules {
#SerializedName("id")
#Expose
private String id;
#SerializedName("name")
#Expose
private String name;
#SerializedName("isEntity")
#Expose
private String isEntity;
#SerializedName("label")
#Expose
private String label;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIsEntity() {
return isEntity;
}
public void setIsEntity(String isEntity) {
this.isEntity = isEntity;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getSingular() {
return singular;
}
public void setSingular(String singular) {
this.singular = singular;
}
#SerializedName("singular")
#Expose
private String singular;
}
GetLoginListDetails .java:
public class GetLoginListDetails {
#SerializedName("session")
#Expose
private String session;
#SerializedName("userid")
#Expose
private String userid;
#SerializedName("vtiger_version")
#Expose
private String vtiger_version;
#SerializedName("mobile_module_version")
#Expose
private String mobile_module_version;
public String getSession() {
return session;
}
public void setSession(String session) {
this.session = session;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getVtiger_version() {
return vtiger_version;
}
public void setVtiger_version(String vtiger_version) {
this.vtiger_version = vtiger_version;
}
public String getMobile_module_version() {
return mobile_module_version;
}
public void setMobile_module_version(String mobile_module_version) {
this.mobile_module_version = mobile_module_version;
}
}
Expected output:
{
"success": true,
"result": {
"login": {
"userid": "1",
"session": "fa000f0a6c5a414e62dcc4cbf99175d6",
"vtiger_version": "5.2.0",
"mobile_module_version": "1.2.1"
},
"modules": [
{
"id": "1",
"name": "Calendar",
"isEntity": true,
"label": "Calendar",
"singular": "To Do"
},
{
"id": "2",
"name": "Leads",
"isEntity": true,
"label": "Leads",
"singular": "Lead"
},
{
"id": "3",
"name": "Accounts",
"isEntity": true,
"label": "Accounts",
"singular": "Account"
}]
}
}
According json response, it return Results instead of List. Change your LoginAndFetchModules like below:
public class LoginAndFetchModules {
#SerializedName("success")
private String success;
#SerializedName("result")
private Results result;
public Results getResult() {
return result;
}
public void setResult(Results result) {
this.result = result;
}
public String getSuccess() {
return success;
}
public void setSuccess(String success) {
this.success = success;
}
}
And then parse the informations like below:
if (response.isSuccessful()) {
LoginAndFetchModules loginAndFetchModules = response.body();
String success = loginAndFetchModules.getSuccess();
if (success.equals("true")) {
Results results = loginAndFetchModules.getResult();
//parse login details
GetLoginListDetails loginDetails = results.getLogin();
String userId = loginDetails.getUserid();
//parse modules
ArrayList<LoginListForModules> modules = results.getModules();
//parse module information
for(LoginListForModules module: modules) {
String id = module.getId();
String name = module.getName();
...
}
}
}
you can create classes for response like this
first class is ResultReponse
public class ResultReponse implements Serializable {
#SerializedName("success")
private boolean success;
#SerializedName("result")
private ResultBean result;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public ResultBean getResult() {
return result;
}
public void setResult(ResultBean result) {
this.result = result;
}
}
ResultBean class
public class ResultBean implements Serializable{
#SerializedName("login")
private LoginBean login;
#SerializedName("modules")
private List<ModulesBean> modules;
public LoginBean getLogin() {
return login;
}
public void setLogin(LoginBean login) {
this.login = login;
}
public List<ModulesBean> getModules() {
return modules;
}
public void setModules(List<ModulesBean> modules) {
this.modules = modules;
}
}
loginBean class inside ResultBean
public class LoginBean implements Serializable {
#SerializedName("userid")
private String userid;
#SerializedName("session")
private String session;
#SerializedName("vtiger_version")
private String vtiger_version;
#SerializedName("mobile_module_version")
private String mobile_module_version;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getSession() {
return session;
}
public void setSession(String session) {
this.session = session;
}
public String getVtiger_version() {
return vtiger_version;
}
public void setVtiger_version(String vtiger_version) {
this.vtiger_version = vtiger_version;
}
public String getMobile_module_version() {
return mobile_module_version;
}
public void setMobile_module_version(String mobile_module_version) {
this.mobile_module_version = mobile_module_version;
}
}
ModulesBean class in list
public class ModulesBean implements Serializable {
#SerializedName("id")
private String id;
#SerializedName("name")
private String name;
#SerializedName("isEntity")
private boolean isEntity;
#SerializedName("label")
private String label;
#SerializedName("singular")
private String singular;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isIsEntity() {
return isEntity;
}
public void setIsEntity(boolean isEntity) {
this.isEntity = isEntity;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getSingular() {
return singular;
}
public void setSingular(String singular) {
this.singular = singular;
}
}
I have json data regarding student details,That i want to print in respective textviews. i'm new to json services please help me to print the this data on screen.I'm using getters and setters for subject score so further i want to use them dynamically.
here is my json data
{
"studentInfo": {
"studentName": "srini#gmail.com",
"studentId": "abc",
"date": 14102017,
"JanuaryScoreCard" : {
"english" : "44",
"Science" : "45",
"maths": "66",
"social" : "56",
"hindi" : "67",
"kannada" : "78",
},
"MarchScoreCard" : {
" english " : "54",
" Science " : "56",
" maths ": "70",
" social " : "87",
" hindi " : "98",
" kannada " : "56"
},
"comments" : ""
}
I'm Something to print but could not,i don't where i'm going wrong
public void init()
{
try {
parseJSON();
} catch (JSONException e)
{
e.printStackTrace();
}
}
public void parseJSON() throws JSONException{
jsonObject = new JSONObject(strJson);
JSONObject object = jsonObject.getJSONObject("studentInfo");
patientName = object.getString("studentName");
patientID = object.getString("studentId");
mName.setText(studentName);
mUserId.setText(studentId);
}
You can use JSON parser and then you can print any data you want from that,
Use GSON for this, here is an example https://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
Here is a basic JSON Parsing process
public void parseJson() {
String your_response = "replace this with your response";
try {
JSONObject jsonObject = new JSONObject(your_response);
JSONObject studentInfoJsonObject = jsonObject.getJSONObject("studentInfo");
StudentInfo studentInfo1 = new StudentInfo();
studentInfo1.setStudentName(studentInfoJsonObject.optString("studentName"));
studentInfo1.setStudentId(studentInfoJsonObject.optString("studentId"));
studentInfo1.setDate(studentInfoJsonObject.optString("date"));
studentInfo1.setComments(studentInfoJsonObject.optString("comments"));
JSONObject januaryScoreCardJsonObject = studentInfoJsonObject.optJSONObject("JanuaryScoreCard");
JanuaryScoreCard januaryScoreCard1 = new JanuaryScoreCard();
januaryScoreCard1.setEnglish(januaryScoreCardJsonObject.optString("english"));
januaryScoreCard1.setHindi(januaryScoreCardJsonObject.optString("hindi"));
januaryScoreCard1.setMaths(januaryScoreCardJsonObject.optString("maths"));
januaryScoreCard1.setSocial(januaryScoreCardJsonObject.optString("social"));
januaryScoreCard1.setKannada(januaryScoreCardJsonObject.optString("kannada"));
januaryScoreCard1.setScience(januaryScoreCardJsonObject.optString("Science"));
JSONObject marchScoreCardJsonObject = studentInfoJsonObject.optJSONObject("JanuaryScoreCard");
MarchScoreCard marchScoreCard = new MarchScoreCard();
marchScoreCard.setEnglish(marchScoreCardJsonObject.optString("english"));
marchScoreCard.setHindi(marchScoreCardJsonObject.optString("hindi"));
marchScoreCard.setMaths(marchScoreCardJsonObject.optString("maths"));
marchScoreCard.setSocial(marchScoreCardJsonObject.optString("social"));
marchScoreCard.setKannada(marchScoreCardJsonObject.optString("kannada"));
marchScoreCard.setScience(marchScoreCardJsonObject.optString("Science"));
studentInfo1.setJanuaryScoreCard(januaryScoreCard1);
studentInfo1.setMarchScoreCard(marchScoreCard);
} catch (JSONException e) {
e.printStackTrace();
}
}
Student Info Class
public class StudentInfo {
private String studentName;
private String studentId;
private String date;
private String comments;
private JanuaryScoreCard januaryScoreCard;
private MarchScoreCard marchScoreCard;
public JanuaryScoreCard getJanuaryScoreCard() {
return januaryScoreCard;
}
public void setJanuaryScoreCard(JanuaryScoreCard januaryScoreCard) {
this.januaryScoreCard = januaryScoreCard;
}
public MarchScoreCard getMarchScoreCard() {
return marchScoreCard;
}
public void setMarchScoreCard(MarchScoreCard marchScoreCard) {
this.marchScoreCard = marchScoreCard;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
}
and Here is January class
public class JanuaryScoreCard {
private String english;
private String Science;
private String maths;
private String kannada;
private String social;
private String hindi;
public String getEnglish() {
return english;
}
public void setEnglish(String english) {
this.english = english;
}
public String getScience() {
return Science;
}
public void setScience(String science) {
Science = science;
}
public String getMaths() {
return maths;
}
public void setMaths(String maths) {
this.maths = maths;
}
public String getKannada() {
return kannada;
}
public void setKannada(String kannada) {
this.kannada = kannada;
}
public String getSocial() {
return social;
}
public void setSocial(String social) {
this.social = social;
}
public String getHindi() {
return hindi;
}
public void setHindi(String hindi) {
this.hindi = hindi;
}
}
and Here is March Class
public class MarchScoreCard{
private String english;
private String Science;
private String maths;
private String kannada;
private String social;
private String hindi;
public String getEnglish() {
return english;
}
public void setEnglish(String english) {
this.english = english;
}
public String getScience() {
return Science;
}
public void setScience(String science) {
Science = science;
}
public String getMaths() {
return maths;
}
public void setMaths(String maths) {
this.maths = maths;
}
public String getKannada() {
return kannada;
}
public void setKannada(String kannada) {
this.kannada = kannada;
}
public String getSocial() {
return social;
}
public void setSocial(String social) {
this.social = social;
}
public String getHindi() {
return hindi;
}
public void setHindi(String hindi) {
this.hindi = hindi;
}
}
You need to parse this json Data , For this you need Create appropriate bean classes and put data while parsing json into this bean classes object and create List .
Then you need to create ListView and Adapter for populate data into Screen or Activity.
Trying to convert a POJO to a Json representation , my output is quite surprising : empty !
Here the POJO class :
public class AccountDTO extends BasicDBObject {
/**
*
*/
public static final String COLLECTION_NAME = "account-data";
private static final long serialVersionUID = 1L;
private String customerFirstName;
private String customerLastName;
private long customerId;
private String IBAN;
private float balance;
private String accountCurrency;
public AccountDTO(Account account, Customer customer) {
super();
this.customerFirstName = customer.getFirstname();
this.customerLastName = customer.getLastname();
this.customerId = customer.getCustomerId();
this.IBAN = account.getIBAN();
this.balance = account.getBalance();
this.accountCurrency = account.getAccountCurrency();
}
public String getCustomerName() {
return customerFirstName;
}
public void setCustomerName(String customerName) {
this.customerFirstName = customerName;
}
public String getCustomerLastName() {
return customerLastName;
}
public void setCustomerLastName(String customerLastName) {
this.customerLastName = customerLastName;
}
public long getCustomerId() {
return customerId;
}
public void setCustomerId(long customerId) {
this.customerId = customerId;
}
public String getIBAN() {
return IBAN;
}
public void setIBAN(String iBAN) {
IBAN = iBAN;
}
public float getBalance() {
return balance;
}
public void setBalance(float balance) {
this.balance = balance;
}
public String getCustomerFirstName() {
return customerFirstName;
}
public void setCustomerFirstName(String customerFirstName) {
this.customerFirstName = customerFirstName;
}
public String getAccountCurrency() {
return accountCurrency;
}
public void setAccountCurrency(String accountCurrency) {
this.accountCurrency = accountCurrency;
}
#Override
public String toString() {
return "AccountDTO [customerFirstName=" + customerFirstName + ", customerLastName=" + customerLastName
+ ", customerId=" + customerId + ", IBAN=" + IBAN + ", balance=" + balance + ", accountCurrency="
+ accountCurrency + "]";
}
}
The converter :
public abstract class AccountDTODigester {
public static String digestJavaToJson(AccountDTO dto){
Gson gson = new Gson();
String json = gson.toJson(dto);
return json;
}
}
Code with jackson :
public abstract class AccountDTODigester {
public static String digestJavaToJson(AccountDTO dto) throws JsonProcessingException{
ObjectMapper mapper = new ObjectMapper();
String jsonInString = new String();
jsonInString = mapper.writeValueAsString(dto);
return jsonInString;
}
}
And finnaly the runner :
public class DAOTest {
AccountDTO accountDTO;
#Before
public void initialize(){
Account account = new Account("FRkk BBBB BGGG GGCC CCCC CCCC CKK", 0, "euro");
Customer customer = new Customer("XXXXXX", "YYYYYY", 1, account);
this.accountDTO = new AccountDTO(account, customer);
}
#Test
public void toJson(){
Assert.assertNotEquals(AccountDTODigester.digestJavaToJson(accountDTO),new String("{}"));
}
Console output :
AccountDTO [customerFirstName=XXXXXX, customerLastName=YYYYYY, customerId=1, IBAN=FRkk BBBB BGGG GGCC CCCC CCCC CKK, balance=0.0, accountCurrency=euro]
{}
When I run the test, my json string is { }and my test is mark as failed.
Gson seems to ver very easy to use, I don't understand why I got this empty Json instead a String filled with a json representation of my AccountDTO object
Ok I found solution :
My JSON was null because extends BasicDBObject , the serialization seems to be jammed by this.
Still looking for a better explication, but now my json is ok.
I am implementing web service using Jersey javax web services. I am sending json object from Postman rest client to my web service but i am not able to handle that json in my web service.
#POST
#Path("/login")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Person login(Person person) throws ClassNotFoundException, SQLException {
Person todo = new Person();
todo.setValid_user(true);
return todo;
}
and i am sending json to above function from postman rest client as given below.
http://localhost:8080/RestWebService/rest/person/login
in raw i am writing my json input as
{"Person":[
"valid_user":"true"
"userId":"1",
"UserName":"parag",
"userPassword":"parag123",
"userLocation":"asd",
"userTime":"asda",
"message":"asdasd"
]}
but i am getting 500 and 415 error.
Hel me out please.
Person class:
package com.avilyne.rest.model;
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;
#XmlRootElement
public class Person {
#JsonProperty("userId")
private String userId;
#JsonProperty("UserName")
private String UserName;
#JsonProperty("userPassword")
private String userPassword;
#JsonProperty("userLocation")
private String userLocation;
#JsonProperty("userTime")
private String userTime;
#JsonProperty("message")
private String message;
#JsonProperty("valid_user")
private String valid_user;
public String isValid_user() {
return valid_user;
}
public void setValid_user(String valid_user) {
this.valid_user = valid_user;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserLocation() {
return userLocation;
}
public void setUserLocation(String userLocation) {
this.userLocation = userLocation;
}
public String getUserTime() {
return userTime;
}
public void setUserTime(String userTime) {
this.userTime = userTime;
}
#Override
public String toString() {
return "Person [userId=" + userId + ", UserName=" + UserName + ", userPassword=" + userPassword
+ ", userLocation=" + userLocation + ", userTime=" + userTime + "]";
}
}
Your JSON is invalid (check it using JSONLint):
A valid JSON to consume in your service would be:
{
"valid_user": true,
"userId": "1",
"UserName": "parag",
"userPassword": "parag123",
"userLocation": "asd",
"userTime": "asda",
"message": "asdasd"
}
Mind the , after "valid_user":"true" and {} instead of [].
You won't need { "Person": {...} }.
Use Jackson mapper, it will automatically convert json to your 'Person' object and vice-versa.
You just need to update your 'Person' bean and add #JsonProperty and #JsonCreator annotation (go through link below) and jackson will take care of the rest.
https://github.com/FasterXML/jackson-annotations
The valid json should be:
{
"valid_user":"true",
"userId":"1",
"userName":"parag",
"userPassword":"parag123",
"userLocation":"asd",
"userTime":"asda",
"message":"asdasd"
}
Edit:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Person {
#JsonProperty("userId")
private String userId;
#JsonProperty("userName")
private String userName;
#JsonProperty("userPassword")
private String userPassword;
#JsonProperty("userLocation")
private String userLocation;
#JsonProperty("userTime")
private String userTime;
#JsonProperty("message")
private String message;
#JsonProperty("valid_user")
private boolean valid_user;
#JsonCreator
public Person(#JsonProperty("userId") String userId,
#JsonProperty("userName") String userName,
#JsonProperty("userPassword") String userPassword,
#JsonProperty("userLocation") String userLocation,
#JsonProperty("userTime") String userTime,
#JsonProperty("message") String message,
#JsonProperty("valid_user") boolean valid_user) {
this.userId = userId;
this.userName = userName;
this.userPassword = userPassword;
this.userLocation = userLocation;
this.userTime = userTime;
this.message = message;
this.valid_user = valid_user;
}
public String getUserId() {
return userId;
}
public String getUserName() {
return userName;
}
public String getUserPassword() {
return userPassword;
}
public String getUserLocation() {
return userLocation;
}
public String getUserTime() {
return userTime;
}
public String getMessage() {
return message;
}
public boolean isValid_user() {
return valid_user;
}
#Override
public String toString() {
return "Person{" +
"userId='" + userId + '\'' +
", userName='" + userName + '\'' +
", userPassword='" + userPassword + '\'' +
", userLocation='" + userLocation + '\'' +
", userTime='" + userTime + '\'' +
", message='" + message + '\'' +
", valid_user=" + valid_user +
'}';
}
}
I updated the above json, you missed ',' after valid_user and also I changed "UserName" to "userName" (change it if you want).
Please test it out and let me know if it's working.