1
To perform the Bean to JSON conversion, vice and versa, I am using the library org.json
for JAVA.
But although an object is converted normally, others end up being loaded with characters \
, when converted to JSON ,and despite no exceptions being fired, no key is recognized.
CONVERT VALUE
public String getUserJSONObject(User bean)
{
JSONObject object = new JSONObject();
JSONObject values = new JSONObject();
try {
values.put("password",bean.getPassword());
values.put("email", bean.getEmail());
values.put("id", bean.getId());
values.put("username", bean.getUsername());
values.put("image", bean.getImage());
values.put("name", bean.getName());
object.put("user",values);
}catch(Exception ex){}
return object.toString();
}
public String getQuestionItemJSONObject(QuestionItem bean)
{
JSONObject object = new JSONObject();
JSONObject values = new JSONObject();
try{
values.put("id",bean.getId());
values.put("value",bean.getValue());
object.put("questionItem",values.toString());
}catch(Exception ex){}
return object.toString();
}
RESULT
{"user":{"id":1,"username":"username","email":"email","name":"name","image":"image","password":"password"}}
{"questionItem":"{\\"id\\":1,\\"value\\":\\"item1\\"}"}
REGAIN VALUE
public User getUser(String jsonString)
{
try{
return getUser(new JSONObject(jsonString));
}catch(Exception ex){return null;}
}
public User getUser(JSONObject object)
{
User bean = new User();
try{
JSONObject values = object;
if (object.has("user"))values = object.getJSONObject("user");
if (values.has("id")) bean.setId(values.getInt("id"));
if (values.has("name")) bean.setName(values.getString("name"));
if (values.has("username")) bean.setUsername(values.getString("username"));
if (values.has("password"))bean.setPassword(values.getString("password"));
if (values.has("email"))bean.setEmail(values.getString("email"));
if (values.has("image"))bean.setImage(values.getString("image"));
}catch(Exception ex){}
return bean;
}
public QuestionItem getQuestionItem(String jsonString)
{
try{
return getQuestionItem(new JSONObject(jsonString));
}catch(Exception ex){return null;}
}
public QuestionItem getQuestionItem(JSONObject object)
{
QuestionItem bean = new QuestionItem();
try{
JSONObject values = object;
if(object.has("questionItem")) values = object.getJSONObject("questionItem");
if(values.has("id"))bean.setId(values.getInt("id"));
if(values.has("value"))bean.setValue(values.getString("value"));
}catch(Exception ex){}
return bean;
}
RESULT
The first bean presented no problem , the second recognized no key, some Beans even present numerous bars, for example:
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\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":4,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"value\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"item4\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}\\\\\\\\\\\\\\"]\\\\\\"}\\",\\"value\\":\\"value\\",\\"minItems\\":2,\\"maxItems\\":5,\\"media\\":\\"media\\"}"}
replace(" ","") is a good ? , because some have more characters \ ?
– user5020
No, you have to nest the objects correctly instead of trying to add object strings inside other objects (see my example using RI of JSR 353).
– Anthony Accioly
And some Beans are converted to json with an absurd amount of
\
because the parser is suffering trying to wring and unclog these strings. The more times you try to do this, the more\
will have at the end value.– Anthony Accioly