我正在寻找一种方式,json解析将按原样获取信息(就好像它是CDATA) - 而不是尝试序列化它。 我们使用.net和java(客户端和服务器) - 所以答案应该是关于JSON结构 有没有办法实现这种结构?
感谢。
答案 0 :(得分:9)
JSON中没有XML CDATA等效项。但是您可以使用类似base64的字符串对字符串文字进行编码。有关详细信息,请参阅this question。
答案 1 :(得分:0)
这是拉曼上述建议的发展。
我喜欢JSON格式,但是我想做两件事,但不能做到:
此线程与这两个问题都有密切关系。
我提议以以下方式克服这一点,但这不会破坏JSON的正式定义,我想知道是否会出现任何问题吗?
如下定义JSON兼容的字符串格式:
"<![CDATA[ (some text, escaped according to JSON rules) ]]>"
用我最喜欢的编程语言编写一个Unescape例程,该例程可以对<![CDATA[ and ]]>
之间的所有内容进行转义。在向我的文本编辑器提供任何JSON文件之前,将调用此方法。
在编辑文件后编写互补例程以进行调用,该过程将根据JSON规则重新转义<![CDATA[ and ]]>
之间的任何内容。
然后,为了将任意数据粘贴到文件中,我所需要做的就是在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转换时,也可以使它起作用。 (我接下来将尝试!)
答案 2 :(得分:0)
您可以创建一个 YAML 文件并转换为 JSON。例如:
test.yaml
storage:
files:
- filesystem: root
path: /etc/sysconfig/network/ifcfg-eth0
mode: 644
overwrite: true
contents:
source: |
data:,
IPV6INIT=yes
IPV6_AUTOCONF=yes
...然后运行 yaml2json_pretty
(稍后显示),如下所示:
#!/bin/bash
cat test.yaml | yaml2json_pretty > test.json
...产生:
test.json
{
"storage": {
"files": [
{
"filesystem": "root",
"path": "/etc/sysconfig/network/ifcfg-eth0",
"mode": 644,
"overwrite": true,
"contents": {
"source": "data:,\nIPV6INIT=yes\nIPV6_AUTOCONF=yes\n"
}
}
]
}
}
这是yaml2json_pretty的源代码:
#!/usr/bin/env python3
import sys, yaml, json
print(json.dumps(yaml.load(sys.stdin.read(),Loader=yaml.FullLoader), sort_keys=False, indent=2))
更多与此yaml2json_pretty
类似的技巧:http://github.com/frgomes/bash-scripts
答案 3 :(得分:-2)
http://www.json.org/详细介绍了JSON格式。根据它,JSON不支持“类似CDATA”的值类型。
要实现CDATA结构,您可以应用自定义逻辑来处理基于字符串的值(并以与.net和Java实现相同的方式执行)。 E.g。
{
"type" : "CDATA",
"value" : "Value that I will handle with my custom logic on java and .net side"
}