When converting an object to JSON, because the occurrence of several characters "\"

1

To perform the Bean conversion to JSON, vice versa, I am using the org.json library for JAVA. However, although one object is normally converted, others end up being loaded with \ characters when converted to JSON, and although no exception is raised, 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\"}"}

RECOVER 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 had no problem, the second did not recognize any key, some beans come up with numerous bars, for example:

{"question":"{\"id\":1,\"correct\":\"{\\\"questionItem\\\":\\\"{\\\\\\\"id\\\\\\\":4,\\\\\\\"value\\\\\\\":\\\\\\\"item4\\\\\\\"}\\\"}\",\"items\":\"{\\\"questionItemList\\\":\\\"[\\\\\\\"{\\\\\\\\\\\\\\\"questionItem\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"id\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":1,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"value\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"item1\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}\\\\\\\\\\\\\\\"}\\\\\\\",\\\\\\\"{\\\\\\\\\\\\\\\"questionItem\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"id\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":2,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"value\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"item2\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}\\\\\\\\\\\\\\\"}\\\\\\\",\\\\\\\"{\\\\\\\\\\\\\\\"questionItem\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"id\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":3,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"value\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"item3\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}\\\\\\\\\\\\\\\"}\\\\\\\",\\\\\\\"{\\\\\\\\\\\\\\\"questionItem\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"id\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":4,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"value\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"item4\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}\\\\\\\\\\\\\\\"}\\\\\\\"]\\\"}\",\"value\":\"value\",\"minItems\":2,\"maxItems\":5,\"media\":\"media\"}"}
    
asked by anonymous 07.10.2014 / 00:43

1 answer

3

The problem is on the line:

object.put("questionItem",values.toString());

You are instructing the library to convert the values to a String (containing " quotes) and place them in the questionItem property. Json is escaping quotes with the character \ .

See that Java EE 7 already has an implementation of JsonObject , as well as a simple way to create nested objects. If you need a Standalone API, just embed RI in your project.

JsonObject model = Json.createObjectBuilder()
   .add("user", Json.createObjectBuilder()
      .add("id", user.getId())
      .add("username", user.getPassword())
      .add("password", user.getPassword())
   .add("questionItem", Json.createObjectBuilder()
         .add("id", question.getId())
         .add("value", question.getItem())
   .build();
    
07.10.2014 / 00:49