Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSON-是否有任何XML CDATA等价物?_Java_Json_.net 4.0_Cdata - Fatal编程技术网

Java JSON-是否有任何XML CDATA等价物?

Java JSON-是否有任何XML CDATA等价物?,java,json,.net-4.0,cdata,Java,Json,.net 4.0,Cdata,我正在寻找一种json解析可以按原样接收信息的方法(就像CDATA一样),而不是试图序列化它。 我们同时使用.net和java(客户机和服务器)——因此答案应该是关于JSON结构的 有没有办法实现这种结构 谢谢。详细介绍了JSON格式。根据它的说法,JSON不支持“类似CDATA”的值类型 要实现CDATA结构,您可以应用自定义逻辑来处理基于字符串的值(并以与.net和java实现相同的方式进行处理)。例如 JSON中没有XML CDATA等价物。但是您可以使用base64之类的东西将消息编码为

我正在寻找一种json解析可以按原样接收信息的方法(就像CDATA一样),而不是试图序列化它。 我们同时使用.net和java(客户机和服务器)——因此答案应该是关于JSON结构的 有没有办法实现这种结构

谢谢。

详细介绍了JSON格式。根据它的说法,JSON不支持“类似CDATA”的值类型

要实现CDATA结构,您可以应用自定义逻辑来处理基于字符串的值(并以与.net和java实现相同的方式进行处理)。例如


JSON中没有XML CDATA等价物。但是您可以使用base64之类的东西将消息编码为字符串文字。有关更多详细信息,请参阅。

这是对上述Raman建议的发展

我喜欢JSON格式,但有两件事我想用它做,但不能:

  • 使用文本编辑器将一些任意文本粘贴到值中
  • 如果XML包含CDATA节,则在XML和JSON之间进行透明转换
  • 这条线索与这两个问题密切相关

    我建议通过以下方式克服这个问题,这不会破坏JSON的正式定义,我想知道如果我这样做,是否会存储任何问题

  • 定义与JSON兼容的字符串格式,如下所示:

  • 用我最喜欢的编程语言编写一个Unescape例程,它可以对
    之间的任何内容进行Unescape。这将在向我的文本编辑器提供任何JSON文件之前调用

  • 编写在编辑文件后调用的补充例程,根据JSON规则重新转义
    之间的任何内容

  • 然后,为了将任意数据粘贴到文件中,我需要做的就是在JSON字符串中键入
    以指示任意数据的开始和结束

    在Python3中,这是在文本编辑前后调用的例程: 朗蟒3

    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")
    
    escape\u列表={
    8.‘b’,
    9:‘t’,
    10:‘n’,
    12:‘f’,
    13:‘r’,
    34: '"',
    }#要转义的ASCII字符代码列表及其转义等价物
    escape\u char=“\\”\必须单独处理此问题
    不太可能_string=“zzffggqww”
    shebang=“#”/json/unesc\n“
    开始\u cdata=“”
    def escapejson(json_路径):
    如果(os.path.isfile(json_path)):#如果它不存在,我们无法更新它
    将open(json_路径)作为json_放入:
    data_in=json_in.read()#使用read()'因为我们要将其视为txt
    #设定逃逸方向
    如果(data_in[:len(shebang)]==shebang):#数据未转义,则重新转义
    data_in=data_in[len(shebang):]
    unescape=False
    data_out=“”
    其他:
    数据输出=shebang
    unescape=True
    while(data_in!=“”):#尽管仍有一些输入需要处理
    x=数据\u in.find(开始\u cdata)
    x1=查找中的数据(结束cdata)
    如果(x>-1):#有些东西需要逃逸
    
    如果(x1,您可以创建YAML文件并转换为JSON。例如:

    test.yaml

    存储:
    文件夹:
    -文件系统:根
    路径:/etc/sysconfig/network/ifcfg-eth0
    模式:644
    覆盖:真
    内容:
    资料来源:|
    数据:,
    IPV6INIT=是
    IPV6\u AUTOCONF=yes
    
    …然后运行
    yaml2json_pretty
    (稍后显示),如下所示:

    !/bin/bash
    cat test.yaml | yaml2json_pretty>test.json
    
    …产生:

    test.json

    {
    “储存”:{
    “文件”:[
    {
    “文件系统”:“根”,
    “路径”:“/etc/sysconfig/network/ifcfg-eth0”,
    “模式”:644,
    “覆盖”:true,
    “内容”:{
    “源”:“数据:,\nIPV6INIT=yes\nIPV6\u AUTOCONF=yes\n”
    }
    }
    ]
    }
    }
    
    这是yaml2json_pretty的源代码:

    !/usr/bin/env python3
    导入sys、yaml、json
    打印(json.dumps(yaml.load(sys.stdin.read(),Loader=yaml.FullLoader),sort_keys=False,indent=2))
    

    更多类似于此的技巧
    yaml2json_pretty
    at:

    将二进制数据放入JSON似乎是用Base64编码实现的最好/最容易的方法。参考:。尽管这可能是最好的解决方案,但这仍然是一个糟糕的主意。JSON应该是人类可读和机器可读的(就像xml一样),而编码打破了这种模式。此外,字符串可能会变得非常大。这当然会失败,因为“value”不能包含文本数据而不转义某些字符。关键是要能够将原始字符串的值放入,如
    value是带有双引号的,如“and:an more”“
    不幸的是,StackOverflow的编辑器删除了我的CDATA字符串,因此没有意义。这里还是一样,添加了一些空格:我错过了什么吗?一点也没有,Yunnosch!你和我都在做同样的事情!:-)谢谢你的帮助。我在上面的代码中发现了一个小错误:if(unescape):。。。。else:应该是:`to_fix=to_fix.replace(escape_char+escape_char,不太可能的字符串)\\双引号表示escape_列表中每个ascii的不可能字符串:to_fix=to_fix.replace(escape_char+escape_list[each_ascii],chr(each_ascii))to_fix=to_fix.replace(不太可能的字符串,escape_char)`。。。如果不太可能的字符串是一些不太可能出现在dataChris中的长字符串值,那么您应该真正接受对您的帖子进行评论而不是对其进行评论的想法。
    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")
    
    {
        "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!]]>"
    }