Are line feeds allowed in JSON strings?

六月ゝ 毕业季﹏ 提交于 2021-01-28 05:06:31

问题


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', '{', '['

回答1:


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:

  • How should I escape strings in JSON? [Java]
  • In C# how to encode XML to output it inside JSON in the JavaScript part of a page


来源:https://stackoverflow.com/questions/33566169/are-line-feeds-allowed-in-json-strings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!