JSON - 是否有任何等效的 XML CDATA?
我正在寻找一种json解析将信息按原样获取(就好像它是CDATA)的方法 - 而不是尝试序列化它。我们同时使用.net和java(客户端和服务器) - 所以答案应该是关于JSON结构有没有办法实现这种结构?
谢谢。
我正在寻找一种json解析将信息按原样获取(就好像它是CDATA)的方法 - 而不是尝试序列化它。我们同时使用.net和java(客户端和服务器) - 所以答案应该是关于JSON结构有没有办法实现这种结构?
谢谢。
这是上述拉曼建议的一个发展。
我喜欢JSON格式,但有两件事我希望能够用它来做,但不能:
此线程与这两个问题密切相关。
我建议通过以下方式克服这个问题,这不会破坏JSON的正式定义,我想知道如果我这样做,我是否会存储任何问题?
定义与 JSON 兼容的字符串格式,如下所示:
"<![CDATA[ (some text, escaped according to JSON rules) ]]>"
用我最喜欢的编程语言编写一个 Unescape 例程,它取消了 .在向我的文本编辑器提供任何JSON文件之前,将调用它。<![CDATA[ and ]]>
编写补充例程以在编辑文件后调用,这会根据 JSON 规则重新转义两者之间的任何内容。<![CDATA[ and ]]>
然后,为了将任何任意数据粘贴到文件中,我需要做的就是通过在JSON字符串后键入来发出JSON字符串中任意数据的开始和结束信号。<![CDATA[ before and ]]>
这是在Python3中文本编辑之前和之后调用的例程:lang-python3
escape_list = {
8 : 'b',
9 : 't',
10: 'n',
12: 'f',
13: 'r',
34: '"',
} #List of ASCII character codes to escape, with their escaped equivalents
escape_char = "\\" #this must be dealt with separately
unlikely_string = "ZzFfGgQqWw"
shebang = "#!/json/unesc\n"
start_cdata = "<![CDATA["
end_cdata = "]]>"
def escapejson(json_path):
if (os.path.isfile(json_path)): #If it doesn't exist, we can't update it
with open(json_path) as json_in:
data_in = json_in.read() #use read() 'cos we're goint to treat as txt
#Set direction of escaping
if (data_in[:len(shebang)] == shebang): #data is unescaped, so re-escape
data_in = data_in[len(shebang):]
unescape = False
data_out = ""
else:
data_out = shebang
unescape = True
while (data_in != ""): #while there is still some input to deal with
x = data_in.find(start_cdata)
x1 = data_in.find(end_cdata)
if (x > -1): #something needs escaping
if (x1 <0):
print ("Unterminated CDATA section!")
exit()
elif (x1 < x): #end before next start
print ("Extra CDATA terminator!")
exit()
data_out += data_in[:x]
data_in = data_in[x:]
y = data_in.find(end_cdata) + len(end_cdata)
to_fix = data_in[:y] #this is what we're going to (un)escape
if (to_fix[len(start_cdata):].find(start_cdata) >= 0):
print ("Nested CDATA sections not supported!")
exit()
data_in = data_in[y:] #chop data to fix from front of source
if (unescape):
to_fix = to_fix.replace(escape_char + escape_char,unlikely_string)
for each_ascii in escape_list:
to_fix = to_fix.replace(escape_char + escape_list[each_ascii],chr(each_ascii))
to_fix = to_fix.replace(unlikely_string,escape_char)
else:
to_fix = to_fix.replace(escape_char,escape_char + escape_char)
for each_ascii in escape_list:
to_fix = to_fix.replace(chr(each_ascii),escape_char + escape_list[each_ascii],)
data_out += to_fix
else:
if (x1 > 0):
print ("Termination without start!")
exit()
data_out += data_in
data_in = ""
#Save all to file of same name in same location
try:
with open(json_path, 'w') as outfile:
outfile.write(data_out)
except IOError as e:
print("Writing "+ json_path + " failed "+ str(e))
else:
print("JSON file not found")
对以下合法 JSON 数据进行操作
{
"test": "<![CDATA[\n We can put all sorts of wicked things like\n \\slashes and\n \ttabs and \n \"double-quotes\"in here!]]>"
}
...将产生以下内容:
#!/json/unesc
{
"test": "<![CDATA[
We can put all sorts of wicked things like
\slashes and
tabs and
"double-quotes"in here!]]>"
}
在此表单中,您可以粘贴标记之间的任何文本。再次调用 rountine 会将其更改回原始合法 JSON。
我认为,在使用 CDATA 区域与 XML 进行转换时,也可以使这一点发挥作用。(我接下来要尝试一下!