如何在JSON中使用特殊字符

问题描述 投票:0回答:1

这里我展示了我的项目的示例JSON对象

{
    "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"}]"
}

它在显示名称中有一些特殊字符,如“/”(Billing State / Province)。现在不允许将它转换为JSONArray并由于其中的特殊字符而放入RootJsonObject。我使用了以下代码

RootJsonObject = new JSONObject();
RootJsonObject.put("content",new JSONArray(JsonObject.getString("Column")));

但我需要那个特殊的角色。是否有任何其他方法可以在我的JSONObject中使用“/”或者我可以使用任何其他JSON util。

java json
1个回答
0
投票

我可以看到你已经更新了你的问题,以包含Content属性的有效JSON。

将此字符串反序列化为JSON时,您将需要转义任何特殊字符。

Apache StringEscapeUtils包含escapeJson方法,可以为您执行此操作:

RootJsonObject = new JSONObject();
String jsonArrayString = StringEscapeUtils.escapeJson(JsonObject.getString("Column"));
RootJsonObject.put("content", new JSONArray(jsonArrayString));

我不是100%清楚JsonObject来自或填充,但如果上述方法不起作用,您可能需要在某个地方早些时候逃避内容。

© www.soinside.com 2019 - 2024. All rights reserved.