0

I have a Java Spring MVC Web Application. I am trying to implement a form submission where the user will be submitting the form and an email would be sent to the user with the form data. I will receive the form data as JSON string. But the email sent with this data doesn't look readable. I have tried the following code to make the data more readable, but doesn't seem to make much difference:

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(jsonData);
String message = gson.toJson(je);

My JSON String looks something like below:

[ { "name": "FirstName", "value": "Abcd" }, { "name": "LastName", "value": "Efgh" }, { "name": "Email", "value": "test.mail@test.com" }, { "name": "PhoneNumber", "value": "9876543210" }, { "name": "Selected Locations", "value": " loc1, loc2, loc3" }, { "name": "Message", "value": "Hi" }, { "name": "g-recaptcha-response", "value": "03AMPJSYULxjYiDRiNxcOSVYxXR9F8dX5pqIYAZ6_F2igAwGvanS4Vh1Lm47ByS2qGELQ9W1h" } ]

I am looking for an option at least to bring name-value pair into separate lines so that it is in some readable manner when received through an email. Is there any way to do this. The form can be generic, SO I may not be able to map this string to any Java object.

I have already tried the following link and the solution does not work for me: Pretty-Print JSON in Java

I also don't see many helpful details online regarding this. I am trying to put each name-value pair in a new line of the generated string to send the data as email.

Geo Thomas
  • 1,139
  • 3
  • 26
  • 59
  • @ Scary Wombat, this question is different, I am trying to format the string to separate lines for each key-value pair, not just pretty print it – Geo Thomas Jan 17 '18 at 06:58
  • so what have you tried? As I see it, you have a List of Objects, If you have a toString method in the Object you could print it as you want – Scary Wombat Jan 17 '18 at 07:13
  • 1
    When you print `message` you get this long line of data?! You can use a loop or a regex to separate the lines and indent, but the `Gson` method is so much simpler and works for me perfectly. – GalAbra Jan 17 '18 at 07:20
  • @Scary Wombat, I have a string of objects, but it can change always. I am trying to implement a generic code. The string is generated based on the submitted form – Geo Thomas Jan 17 '18 at 07:35

0 Answers0