When converting an object to JSON, because it appears several characters " "

Asked

Viewed 827 times

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:

{"question":"{\\"id\\":1,\\"correct\\":\\"{\\\\\\"questionItem\\\\\\":\\\\\\"{\\\\\\\\\\\\\\"id\\\\\\\\\\\\\\":4,\\\\\\\\\\\\\\"value\\\\\\\\\\\\\\":\\\\\\\\\\\\\\"item4\\\\\\\\\\\\\\"}\\\\\\"}\\",\\"items\\":\\"{\\\\\\"questionItemList\\\\\\":\\\\\\"[\\\\\\\\\\\\\\"{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"questionItem\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"id\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":1,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"value\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"item1\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}\\\\\\\\\\\\\\",\\\\\\\\\\\\\\"{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"questionItem\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"{\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"id\\\\\\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\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"id\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":4,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"value\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"item4\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}\\\\\\\\\\\\\\"]\\\\\\"}\\",\\"value\\":\\"value\\",\\"minItems\\":2,\\"maxItems\\":5,\\"media\\":\\"media\\"}"}

1 answer

3


The problem is on the line:

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

You are sending the library to convert the values to a String (which contains quotes") and put them on the property questionItem. The 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 a 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();
  • replace(" ","") is a good ? , because some have more characters \ ?

  • 1

    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).

  • 1

    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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.