如何移除JSON字符串中特定值的双引号?(Gson场景)
The issue you're facing is that Gson treats the userinfo value as a plain String (since it's stored as one in your source data), so it wraps it in quotes and escapes inner characters when serializing. To fix this, you need to parse the userinfo string into a proper JSON object first, then include that object in your main data structure before serializing the whole thing.
Here are a few straightforward approaches using Gson:
Approach 1: Using JsonObject for Flexibility
If you don't want to create dedicated POJOs, you can use Gson's JsonObject to parse the userinfo string and integrate it into your main JSON structure:
import com.google.gson.Gson; import com.google.gson.JsonObject; public class JsonProcessingExample { public static void main(String[] args) { // Your original JSON string (where userinfo is quoted) String originalJson = "{ \"ctid\": \"78b2aa1e7fb04942bf4b7d4771143f2c\", \"id\": \"d27aa45cc4df4b62bc8e221b17d485c5\", \"uid\": \"\", \"msisdn\": \"9886121212\", \"status\": \"ACTIVE\", \"actualAmount\": \"10.0\", \"currency\": \"USD\", \"userinfo\": \"{\\\"UID\\\":\\\"dfjnbfnofnv61\\\",\\\"TRANSID\\\":\\\"898097261\\\",\\\"COTENT_ID\\\":{\\\"contentid\\\":\\\"3019154\\\",\\\"contentname\\\":\\\"00000004-TheGameofLifeHD03019154\\\",\\\"contenttype\\\":\\\"Game\\\"},\\\"CP_NAME\\\":\\\"FT61\\\"}\" }"; Gson gson = new Gson(); // Step 1: Parse the entire original JSON into a JsonObject JsonObject mainJsonObject = gson.fromJson(originalJson, JsonObject.class); // Step 2: Extract the userinfo string and parse it into a JsonObject String userInfoString = mainJsonObject.get("userinfo").getAsString(); JsonObject userInfoObject = gson.fromJson(userInfoString, JsonObject.class); // Step 3: Replace the quoted userinfo string with the parsed object mainJsonObject.add("userinfo", userInfoObject); // Step 4: Serialize the modified JsonObject back to JSON String finalJson = gson.toJson(mainJsonObject); System.out.println(finalJson); } }
Approach 2: Using POJOs for Type Safety
For better type safety and maintainability, create dedicated POJO classes that match your JSON structure. This way, Gson will automatically handle the nested serialization:
Step 1: Define Your POJO Classes
import com.google.gson.annotations.SerializedName; // Main data class class TransactionData { private String ctid; private String id; private String uid; private String msisdn; private String status; private String actualAmount; private String currency; private UserInfo userinfo; // This is a nested object, not a String // Getters and Setters (or use Lombok for brevity) } // UserInfo nested class class UserInfo { private String UID; private String TRANSID; @SerializedName("COTENT_ID") private ContentId contentId; private String CP_NAME; // Getters and Setters } // ContentId nested class class ContentId { private String contentid; private String contentname; private String contenttype; // Getters and Setters }
Step 2: Parse and Serialize
import com.google.gson.Gson; public class PojoExample { public static void main(String[] args) { // Original data (could come from a Map, database, etc.) TransactionData transaction = new TransactionData(); transaction.setCtid("78b2aa1e7fb04942bf4b7d4771143f2c"); transaction.setId("d27aa45cc4df4b62bc8e221b17d485c5"); // ... set other fields ... // Parse the userinfo string into a UserInfo object String userInfoStr = "{\"UID\":\"dfjnbfnofnv61\",\"TRANSID\":\"898097261\",\"COTENT_ID\":{\"contentid\":\"3019154\",\"contentname\":\"00000004-TheGameofLifeHD03019154\",\"contenttype\":\"Game\"},\"CP_NAME\":\"FT61\"}"; Gson gson = new Gson(); UserInfo userInfo = gson.fromJson(userInfoStr, UserInfo.class); // Set the parsed UserInfo object instead of the string transaction.setUserinfo(userInfo); // Serialize the entire object to JSON String finalJson = gson.toJson(transaction); System.out.println(finalJson); } }
Approach 3: Modifying a Map of Data
If your source data is in a Map<String, Object> (common for dynamic data), you can update the map to replace the userinfo string with a parsed object:
import com.google.gson.Gson; import com.google.gson.JsonObject; import java.util.HashMap; import java.util.Map; public class MapExample { public static void main(String[] args) { // Original map with userinfo as a string Map<String, Object> dataMap = new HashMap<>(); dataMap.put("ctid", "78b2aa1e7fb04942bf4b7d4771143f2c"); dataMap.put("id", "d27aa45cc4df4b62bc8e221b17d485c5"); // ... add other fields ... dataMap.put("userinfo", "{\"UID\":\"dfjnbfnofnv61\",\"TRANSID\":\"898097261\",\"COTENT_ID\":{\"contentid\":\"3019154\",\"contentname\":\"00000004-TheGameofLifeHD03019154\",\"contenttype\":\"Game\"},\"CP_NAME\":\"FT61\"}"); Gson gson = new Gson(); // Parse userinfo string to JsonObject String userInfoStr = (String) dataMap.get("userinfo"); JsonObject userInfoObj = gson.fromJson(userInfoStr, JsonObject.class); // Update the map dataMap.put("userinfo", userInfoObj); // Serialize to JSON String finalJson = gson.toJson(dataMap); System.out.println(finalJson); } }
All these approaches will produce the desired JSON where userinfo is a nested object instead of a quoted string. The core idea is to parse the userinfo string into a structured object before serializing the entire dataset—Gson will then handle the nested serialization correctly.
内容的提问来源于stack exchange,提问作者devtest

