Json String from Gson: Remove Double Quotes

JSON string from Gson: remove double quotes

It's not documented properly, but JsonElement#toString() gets you a string that represents the JSON element and would be appropriate for re-creating the JSON serialization. What you want is JsonElement#getAsString(). This will throw an error if you're not looking at a string, but if you are, you'll get the string value.

Here's a test program to demonstrate:

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
public class Test {
public static void main(String[] args) {
String in = "{\"hello\":\"world\"}";
System.out.println(in);
JsonElement root = new JsonParser().parse(in);
System.out.println(root.getAsJsonObject().get("hello").toString());
System.out.println(root.getAsJsonObject().get("hello").getAsString());
}
}

And its output:

{"hello":"world"}
"world"
world

GSON getAsString() method does not strip double quotes from JSON element?

What you do now is encode the string again:

val tempObject = JsonObject().apply {
addProperty("a", "b")
}
val r0 = tempObject.get("a").asString // b
val r1 = Gson().toJson(tempObject.get("a")) // "b"
val r2 = Gson().toJson(tempObject.get("a").asString) // "b"
val r3 = Gson().toJson(tempObject.get("a").toString()) // "\"b\""

asString returns the string value just fine. Calling on it toJson() adds the quotes.

How to replace double quotes from a particular value in JSON string?

here is the solution that had worked for me.

             //first take the jsonResponse
JsonElement jsonElement = new JsonParser().parse(jsonResponse);
JsonElement userInfoJsonElement = new JsonParser().parse(paramMap.get("FILTERED_USERINFO"));
//second remove the key-value(String) pair from json
jsonElement.getAsJsonObject().remove("userinfo");
//third add the json element back as key-value(jsonElement)
jsonElement.getAsJsonObject().add("userinfo", userInfoJsonElement);

Build a JSON object in which value should not have quotes using Gson

By not making it a String. This is the only way; not escaping and double-quoting a String would be a wrong and broken behavior.

The problem is that you have JSON with different structures under this key in all 3 cases, so it isn't clear what type it should be instead. You could simply make it JsonElement key4:

public class BuildJson {
private String key1 = "value1";
private String key2 = "value2";
private String key3 = "value3";
private JsonElement key4;

public BuildJson(Object value4) {
key4 = new Gson().toJsonTree(value4);
}

public String buildJson(){
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String new_json = gson.toJson(this);
return new_json;
}
}

And assuming the Strings in key4_object are JSON for objects you want,

List<String> key4_object = ...;
JsonParser parser = new JsonParser();
for (String i : key4_object){
BuildJson bj = new BuildJson(parser.parse(i));
String new_json = bj.buildJson();
System.out.print(gson.toJson(new_json));
}

Gson.toJson adding extra quotes to string value of JSON

I think you are double calling toJson() method which is already a String.
I mean if you call toJson() on "{ids:[2079]}", then it will produce "{\"ids\":[2079]}". So check your object.

Convert Map to String with double quotes escaped using Gson

(Thanks to @fluffy)

Instead of doing :

String myMapAsJsonStringWithDoubleQuotesEscaped = myMapAsJsonString.replace("\"", "\\\"");

I've just reserialized the JSON string once again :

import com.google.gson.Gson;

String myMapAsJsonString = new Gson().toJson(myMap);
// reserialize once again with toJson method
String myMapAsJsonStringWithDoubleQuotesEscaped = new Gson().toJson(myMapAsJsonString);

And got the correct result I was expecting :

System.out.println(myMapAsJsonStringWithDoubleQuotesEscaped);

prints :

"{\"key1\":\"value1\",\"key2\":{\"key2-1\":\"value2-1\",\"key2-2\":22}}"


Related Topics



Leave a reply



Submit