6

I'm trying to create a Json structure manually in Java. In the end, I need the a string representation of my Json object. Since my project already has a dependency to GSON, I wanted to use this. But after several hours of trying and googling, I think, I completely misunderstand something.

At the moment, I have the following (non-working) code:

JsonObject user_auth = new JsonObject();
user_auth.addProperty("user_name", username);
user_auth.addProperty("password", password);
JsonObject rest_data = new JsonObject();
Gson gson = new Gson();
rest_data.addProperty("user_auth", gson.toJson(user_auth));
rest_data.addProperty("application", APPLICATION_NAME);
String payload = gson.toJson(rest_data);

The problem I'm facing is that the "user_auth" element gets escaped quotes (\" instead of " when it is added to the outer element. How can I prevent this? I also tried to use

rest_data.addProperty("user_auth", user_auth.toString());

but this did exactely the same.

Regards,

4
  • 2
    You're doubly-encoding your JSON. Just add user_auth directly to the enclosing JsonObject, don't JSONify it. Commented Jan 3, 2014 at 13:12
  • (Before you do the toJson call your entire object structure should toString to look almost exactly like the intended JSON result.) Commented Jan 3, 2014 at 13:14
  • (Converting JSON to and from Maps/Lists is perfectly natural and uncomplicated. Stuff like Jackson makes it more complicated, not less.) Commented Jan 3, 2014 at 13:16
  • Hot Licks' comment should be an answer, and accepted, as it solves OP's question concisely and completely without editorial comment. (Just remove the "toJson" from user_auth, win!) Commented Feb 27, 2023 at 23:33

4 Answers 4

13

Writing the JSON manually is not a good solution.

It seems that you encode user_auth twice.

Try this instead:

JsonObject user_auth = new JsonObject();
user_auth.addProperty("user_name", username);
user_auth.addProperty("password", password);

JsonObject rest_data = new JsonObject();
rest_data.addProperty("user_auth", user_auth);
rest_data.addProperty("application", APPLICATION_NAME);

Gson gson = new Gson();
String payload = gson.toJson(rest_data);
Sign up to request clarification or add additional context in comments.

1 Comment

Strong disagree. Writing JSON manually is frequently a good solution. Asserted without evidence, dismissed without evidence.
1

Another way is directly convert POJO into JSON.

1. Create POJO object

i.e. user class:

class User {

    @JsonProperty("user_name") // this annotation helps you to specify
                               // property name in result json
    private String userName;

    @JsonProperty("first_name")
    private String firstName;

    // ...

    // getters and setters here, hashcode, equals, etc.

}

2. Convert POJO to JSON string

for example:

User user = new User("superlogin", "superpass", ...);

String jsonString = new Gson().toJson(user);

Thats all :)

I think it's more convenient way than build json structure manually.

Comments

0

Use the addProperty() for the user_auth and then for the rest_data you need the payload.

String payloadJSON = gson.toJson(rest_data);

Comments

-2

If you dont wont to add external jar then, try this

return "{UPLOAD:"+upload+",UPLOADSTATUS:"+perUpload+",TRANSCODE:"+transcode+",TRANSCODESTATUS:"+transcodestatus+"}";

1 Comment

bad advice in long term, imagine if you have 20 properties and 10 entities with manual to-json method (as you suggest). one day you need to add new 5 properties for each object. it's kinda little hell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.