Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8085013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:58:40+00:00 2026-06-05T17:58:40+00:00

Could someone please suggest why this is happening… I’ve got some code to pretty

  • 0

Could someone please suggest why this is happening…

I’ve got some code to pretty print some JSON. To do this, I am making use out of the Gson library.

However, while thus usually works well, some characters don’t seem to be displayed properly. Here is a simple piece of code that demonstrates the problem:

//Creating the JSON object, and getting as String:
JsonObject json = new JsonObject();
JsonObject inner = new JsonObject();
inner.addProperty("value", "xpath('hello')");
json.add("root", inner);
System.out.println(json.toString());

//Trying to pretify JSON String:
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser parser = new JsonParser();
JsonElement je = parser.parse(json.toString());
System.out.println(gson.toJson(je));

The output of the above code is:

{"root":{"value":"xpath('hello')"}}
{
  "root": {
    "value": "xpath(\u0027hello\u0027)"
  }
}

How could I fix the above?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:58:43+00:00Added an answer on June 5, 2026 at 5:58 pm

    Use this code, to create Gson object:

    Gson gs = new GsonBuilder()
        .setPrettyPrinting()
        .disableHtmlEscaping()
        .create();
    

    The disableHtmlEscaping() method tells gson not to escape HTML characters such as <, >, &, =, and a single quote which caused you trouble: '.

    Note, that this may cause trouble, if you render such unescaped JSON into a <script/> tag in HTML page without using additional <![CDATA[ ... ]]> tag.

    You can see how it works, and what other chars are escaped, by looking into the code of JsonWriter class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please give me a example of a function that would use this
Could someone please explain when would I want to use delegation instead of inheritance?
Could someone please suggest a good way of taking a global seed value e.g.
Could someone please explain why the following snippet behaves as it does? l <-
Could someone please help me find out why my attempt to bind Collection to
Could someone please point me to an example JUnit test that uses InMemoryTestContainerFactory ,
Could someone please explain why I'm getting the error: java.lang.RuntimeException: Unable to start activity
Could someone please recommend a simple (as in very easy to understand) charting library
Could someone please advise the current best practice around Date and Calendar types. When
Could someone please point out a site where I can find an algorithm to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.