JSON 字符串中是否允许换行? [英] Are line feeds allowed in JSON strings?

查看:136
本文介绍了JSON 字符串中是否允许换行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 JSON 中为我的 REST OSB 12c 代理发送 XML,如下所示:

<代码>{"登录": "jstein","identityContext": "jazn.com","taskId": "字符串",有效载荷":{any_0":{"any_01": "<afastamento xmlns:ns1='http://www.tjsc.jus.br/soa/schemas/comagis/AfastamentoMagistrado' xsi:type='def: AfastamentoMagistradoType' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://xmlns.oracle.com/bpel/workflow/task'><ns1:Magistrado>719</ns1:Magistrado><ns1:Status>初始</ns1:Status><ns1:Vaga>8770</ns1:Vaga><ns1:Tipo>Licenca Nojo</ns1:Tipo><ns1:PeriodoReferencia/><ns1:DataIncialSolicitada>2015-10-10</ns1:DataIncialSolicitada><ns1:DataFinalSolicitada>2015-11-05</ns1:DataFinalSolicitada></afastamento>"}},结果":开始"}

OSB 12c 将错误发回给我:

<块引用>

 "errorMessage" : "ORABPEL-15235\n\n翻译失败.\n无法翻译JSON 到 XML.org.codehaus.jackson.JsonParseException:非法未加引号字符 ((CTRL-CHAR, code 10)): 必须使用反斜杠转义为包含在字符串值中\n [来源:java.io.BufferedReader@7db921c7;线:7,列:619]\n传入的数据不符合 NXSD 架构.请更正问题.\n"

我正在 JSONLint 测试我的 JSON 请求,它总是给我关于用 < 开始一个字符串的错误:

<块引用>

第 7 行解析错误:"any_01": "<afastamento xmlns:-----------^期待 'STRING, 'NUMBER, 'NULL', 'TRUE', FALSE', '{', '['

解决方案

No,文字换行符 (CTRL-CHAR, code 10) 和换行符是控制字符在 JSON 字符串中

XML 不需要元素之间的换行符.您可以简单地删除它们,将多行 XML 文档更改为等效的单行 XML 文档,该文档可以毫无问题地作为 JSON 字符串传递.或者,您可能需要考虑转义换行符 \n,或者更一般地说,转义整个字符串:

I need to send XML inside a JSON for my REST OSB 12c Proxy as follow:

{
    "login": "jstein",
    "identityContext": "jazn.com",
    "taskId": "string",
    "payload": {
        "any_0": {
            "any_01": "<afastamento xmlns:ns1='http: //www.tjsc.jus.br/soa/schemas/comagis/AfastamentoMagistrado' xsi:type='def: AfastamentoMagistradoType' xmlns:xsi='http: //www.w3.org/2001/XMLSchema-instance' xmlns='http: //xmlns.oracle.com/bpel/workflow/task'>
          <ns1:Magistrado>719</ns1:Magistrado>
          <ns1:Status>Inicial</ns1:Status>
          <ns1:Vaga>8770</ns1:Vaga>
          <ns1:Tipo>Licenca Nojo</ns1:Tipo>
          <ns1:PeriodoReferencia/>
          <ns1:DataInicialSolicitada>2015-10-10</ns1:DataInicialSolicitada>
          <ns1:DataFinalSolicitada>2015-11-05</ns1:DataFinalSolicitada>
  </afastamento>"
        }
    },
    "outcome": "Start"
}

The OSB 12c send me back the error:

 "errorMessage" : "ORABPEL-15235\n\nTranslation Failure.\nFailed to translate
 JSON to XML. org.codehaus.jackson.JsonParseException: Illegal unquoted
 character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be
 included in string value\n at [Source: java.io.BufferedReader@7db921c7; line:
 7, column: 619]\nThe incoming data does not conform to the NXSD schema. Please correct the problem.\n"

I am testing my JSON request at JSONLint, and it always gives me the error about start a String with <:

Parse error on line 7:
"any_01": "<afastamento xmlns:
-----------^
Expecting 'STRING, 'NUMBER, 'NULL', 'TRUE', FALSE', '{', '['

解决方案

No, literal line feeds (CTRL-CHAR, code 10) and newlines are control characters that are not allowed within a JSON string:

XML does not require the line feeds between elements. You can simply remove them, changing your multi-line XML document to an equivalent single-line XML document that will be able to be passed as a JSON string without problem. Or, you may want to consider escaping the line feeds \n, or more generally, escaping the entire string:

这篇关于JSON 字符串中是否允许换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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