You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JSON中保留特殊字符?含斜杠的JSON数组转换问题

Solution for Preserving "/" in JSON Parsing

First, let's clear up a key point: the "/" character itself is not causing your parsing failure—JSON allows "/" in strings without any required escaping. The real issue is the invalid structure of your original JSON, specifically how the Column field is formatted.

Here's how to resolve this step by step:

1. Fix the Original JSON Structure (Cleanest Solution)

If you control the source of the JSON, change the Column field from a quoted string to a native JSON array. This eliminates the need to parse a string entirely:

{ 
  "objectType": "main", 
  "description": "", 
  "Column": [
    {"displayName": "Account Name", "DataType" : "string"}, 
    {"displayName" : "Billing City", "DataType" : "string" }, 
    { "displayName" : "Billing State/Province" , "DataType" : "string" }, 
    {"displayName" : "Billing Street", "DataType" : "textarea"}
  ] 
}

Now you can directly retrieve the array without string parsing:

JSONObject rootJsonObject = new JSONObject();
JSONArray columnArray = jsonObject.getJSONArray("Column");
rootJsonObject.put("content", columnArray);

The "/" in "Billing State/Province" will be preserved automatically.

2. Correct Escaping If Column Must Remain a String

If you can't modify the source JSON and Column has to stay a string, ensure all inner double quotes are properly escaped with \". Your valid JSON should look like this:

{ 
  "objectType": "main", 
  "description": "", 
  "Column": "[{\"displayName\": \"Account Name\", \"DataType\" : \"string\"}, {\"displayName\" : \"Billing City\", \"DataType\" : \"string\" }, { \"displayName\" : \"Billing State/Province\" , \"DataType\" : \"string\" }, {\"displayName\" : \"Billing Street\", \"DataType\" : \"textarea\"}]"
}

With this corrected string, your original code will work as expected:

JSONObject rootJsonObject = new JSONObject();
String columnString = jsonObject.getString("Column");
JSONArray columnArray = new JSONArray(columnString);
rootJsonObject.put("content", columnArray);

The "/" character remains intact because JSON doesn't require escaping it.

3. Switch to a More Robust JSON Library

If you're still encountering issues with the standard org.json library, try using Jackson or Gson—these libraries handle edge cases like malformed strings more gracefully:

Example with Gson:

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

Gson gson = new Gson();
JsonObject originalJson = gson.fromJson(yourJsonString, JsonObject.class);

// Handle both array and string cases for Column
JsonArray columnArray;
if (originalJson.get("Column").isJsonArray()) {
    columnArray = originalJson.getAsJsonArray("Column");
} else {
    String columnString = originalJson.get("Column").getAsString();
    columnArray = gson.fromJson(columnString, JsonArray.class);
}

JsonObject rootJsonObject = new JsonObject();
rootJsonObject.add("content", columnArray);

Example with Jackson:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

ObjectMapper mapper = new ObjectMapper();
JsonNode originalNode = mapper.readTree(yourJsonString);

JsonArray columnArray;
if (originalNode.get("Column").isArray()) {
    columnArray = (JsonArray) originalNode.get("Column");
} else {
    columnArray = mapper.readValue(originalNode.get("Column").asText(), JsonArray.class);
}

JsonObject rootJsonObject = new JsonObject();
rootJsonObject.put("content", columnArray);

Both libraries will preserve the "/" character without any extra configuration.


内容的提问来源于stack exchange,提问作者Karthik Subramani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:55:30