简体   繁体   中英

How to have special character in JSON

Here I have shown the sample JSON object of my project

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

It has some special characters like"/" (Billing State/Province) in the display name. Now it was not allowed to convert it to JSONArray and put in the RootJsonObject due to the special character in it. I had used the following code

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

But I need that special character. Is there is any other way to have that "/" in my JSONObject or can I use any other JSON util.

I can see you've updated your question to include valid JSON for the Content property.

When deserialising this string to JSON, you will need to escape any special characters.

Apache StringEscapeUtils contains an escapeJson method which can do this for you:

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

I am not 100% clear where JsonObject comes from or is populated but if the above does not work, you may need to escape the content earlier in the process somewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM