How to escape the quotes in JSON Object?

11,615

Solution 1

Your problem is that with

jsonObject.addProperty("ap", ap.toString());

you are adding a property which is the String representation of a Set in Java. It has nothing to do with JSON (even if the format looks the same).

You will have to convert your Set into a JsonElement (a JsonArray really but you won't see that).

Create a Gson object somewhere

Gson gson = new Gson();

and use it to convert your Set elements to JsonElement objects and add them to the JsonObject.

jsonObject.add("ap", gson.toJsonTree(ap));
jsonObject.add("bp", gson.toJsonTree(bp));

Gson has its conventions, it converts a Set into a JsonArray which is a sub type of JsonElement and you can therefore add it with JsonObject#add(String, JsonElement).

Solution 2

Use StringEscapeUtils:

import org.apache.commons.lang3.StringEscapeUtils;

(...)

myString = StringEscapeUtils.escapeJson(myString);

On Android, remember to update your app/build.gradle:

compile 'org.apache.commons:commons-lang3:3.4'
Share:
11,615
Admin
Author by

Admin

Updated on June 30, 2022

Comments

  • Admin
    Admin almost 2 years

    Below is my method which makes the JSONObject and then print out the JSONString.

    I am using Google GSON.

    private String generateData(ConcurrentMap<String, Map<Integer, Set<Integer>>> dataTable, int i) {
    
        JsonObject jsonObject = new JsonObject();
    
        Set<Integer> ap = dataTable.get("TEST1").get(i);
        Set<Integer> bp = dataTable.get("TEST2").get(i);
    
        jsonObject.addProperty("description", "test data");
        jsonObject.addProperty("ap", ap.toString());
        jsonObject.addProperty("bp", bp.toString());
    
        System.out.println(jsonObject.toString());
    
        return jsonObject.toString();
    }
    

    Currently if I print out the jsonObject.toString() then it prints out like this -

    {"description":"test data","ap":"[0, 1100, 4, 1096]","bp":"[1101, 3, 6, 1098]"}
    

    But this is not what I need. I want to print out like below which is without double quote on ap and bp values.

    {"description":"test data","ap":[0, 1100, 4, 1096],"bp":[1101, 3, 6, 1098]}
    

    I am not sure how do I escape that quotes in the JSONObject?

  • Admin
    Admin over 10 years
    It doesn't work as well. The method addProperty(String, String) in the type JsonObject is not applicable for the arguments (String, JsonElement)
  • Sotirios Delimanolis
    Sotirios Delimanolis over 10 years
    @SSH add not addProperty.
  • Sotirios Delimanolis
    Sotirios Delimanolis over 10 years
    @SSH Try not to create a Gson object in the method. It will slow everything down as it's a quite heavy initialization. Either pass it as a method argument or reference it with a static variable somewhere.
  • Sotirios Delimanolis
    Sotirios Delimanolis over 10 years
    The quotes were a misunderstanding of what they were adding to the Json object. They wanted a Json array instead of a String which is the toString() result of a Set. See the expected JSON.
  • Aram
    Aram almost 7 years
    Looks like this is the only solution. Only thing we need to change the syntax for android.