是否有可能使用单斜杠将字符串序列化为json? [英] Is it possible to serialize string to json with single slashes?

查看:198
本文介绍了是否有可能使用单斜杠将字符串序列化为json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程

class MyClass{
    String str = "D:\folder1\folder2\folder3\file.txt"
    ....
}

我将MyClass的对象序列化为json。在json中,我发现我的 str 看起来像 D:\\folder1\\folder2\\folder3\\file .txt

I serialize object of MyClass to json. In json I see that my str looks like D:\\folder1\\folder2\\folder3\\file.txt

是否有可能要求GSON不要添加双斜线?

Is it possible to ask GSON don't add double slash?

推荐答案

JSON规范规定

The JSON specification states


所有Unicode字符都可以放在引号
中,除了必须转义的字符:引号,
reverse solidus 和控制字符(U + 0000到U + 001F)。

All Unicode characters may be placed within the quotation marks, except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).

因此,必须转义 \ 以使JSON有效。这是没有办法的。

Therefore the \ must be escaped for the JSON to be valid. There is no way around this.

请注意,解析器在读取JSON时也会删除第二个 \ ,所以它不应该是一个问题。

Note that your parser will also remove the second \ when reading the JSON, so it should not be an issue.

这篇关于是否有可能使用单斜杠将字符串序列化为json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆