英文:
Golang parsing JSON with embeded XML
问题
我有一个HTTP响应体,它是一个JSON,但其中只包含一个字段,即一个作为字符串的XML文档。我不想解析XML,只想提取它,因为我需要将其作为XML发送到其他地方。当我尝试使用以下代码时:
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
var ccr []models.Ccda
err = json.Unmarshal(body, &ccr)
我的模型是这样的:
Ccda struct {
CCDA string `json:"ccda"`
}
我得到了一个错误:"invalid character '<' looking for beginning of value"。
我还尝试了使用字符串映射,但仍然出现相同的错误。
JSON响应的开头是:
[{
"ccda": "<?xml version=\"1.0\"?>\n<ClinicalDocument xmlns=\"urn:hl7-org:v3\"..."}]
ccda是JSON字符串中唯一的元素。再次强调,我不想解析XML。
GO处理转义引号的方式有问题吗?json元素ccda的值是XML字符串。
从供应商网站(他们的工具)查看原始数据,我得到了这个:
[{
"ccda": "<?xml version=\"1.0\"?>\n<ClinicalDocument xmlns=\"urn:hl7-org:v3\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xmlns:sdtc=\"urn:hl7-org:sdtc\">\n <realmCode code=\"US\"\/>\n <typeId root=\"2.16.840.1.113883.1.3\" extension=\"POCD_HD000040\"\/>\n <templateId root=\"2.16.840.1.113883.10.20.22.1.1\"\/>\n <templateId root=\"2.16.840.1.113883.10.20.22.1.2\"\/>\n <id root=\"0cf1aaf6-2016-a8b8-2fd3c-001A64958C30\"\/>\n <code code=\"34133-9\" codeSystem=\"2.16.840.1.113883.6.1\" displayName=\"Summarization of Episode Note\"\/>\n </ClinicalDocument>}]```
当我读取response.Body并将其转换为字符串时,我得到了这个(虽然它是不正确的,但我可能可以使用它):
```xml
<?xml version="1.0"?>
<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sdtc="urn:hl7-org:sdtc">
<realmCode code="US"/>
<typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040"/>
<templateId root="2.16.840.1.113883.10.20.22.1.1"/>
<templateId root="2.16.840.1.113883.10.20.22.1.2"/>
<id root="0cf1a768-2016-505e-2fd3c-001A64958C30"/>
<code code="34133-9" codeSystem="2.16.840.1.113883.6.1" displayName="Summarization of Episode Note"/>
</ClinicalDocument>
当我在GO中进行其他调用以获取其他信息时,所有测试(GO、Ruby和网站工具)都能正确返回JSON,只有这个调用在GO中不能返回正确的JSON。
英文:
I have a http response body that is in JSON however it contains one field and that is an XML document as a string. I do not want to parse the XML at all, I just want to extract it because I need to send it somewhere else as XML. When I attempt to use:
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
var ccr []models.Ccda
err = json.Unmarshal(body, &ccr)
The Model is this:
Ccda struct {
CCDA string `json:"ccda"`
}
I get an err of "invalid character '<' looking for beginning of value"
I have attempted it with string maps also and still same error.
The beginning of the json response is:
[{
"ccda": "<?xml version=\"1.0\"?>\n<ClinicalDocument xmlns=\"urn:hl7-org:v3\"..."
}]
ccda is the only element in the json string. Again, I have no desire to parse the XML.
Is there a problem with the way GO is handling the escaped quotes? The value of the json element ccda is the XML string.
Looking at the raw data from the vendor's site(their tool) I get this:
[{
"ccda": "<?xml version="1.0"?>\n<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sdtc="urn:hl7-org:sdtc">\n <realmCode code="US"/>\n <typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040"/>\n <templateId root="2.16.840.1.113883.10.20.22.1.1"/>\n <templateId root="2.16.840.1.113883.10.20.22.1.2"/>\n <id root="0cf1aaf6-2016-a8b8-2fd3c-001A64958C30"/>\n <code code="34133-9" codeSystem="2.16.840.1.113883.6.1" displayName="Summarization of Episode Note"/>\n </ClinicalDocument>}]
When I read the response.Body and convert to string I get this (it is incorrect, though I may be able to use it):
<?xml version="1.0"?>
<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sdtc="urn:hl7-org:sdtc">
<realmCode code="US"/>
<typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040"/>
<templateId root="2.16.840.1.113883.10.20.22.1.1"/>
<templateId root="2.16.840.1.113883.10.20.22.1.2"/>
<id root="0cf1a768-2016-505e-2fd3c-001A64958C30"/>
<code code="34133-9" codeSystem="2.16.840.1.113883.6.1" displayName="Summarization of Episode Note"/>
<\ClinicalDocument>
When I do other calls for other information in GO, I do get back the proper JSON on all tests go/ruby/site tools. just not on this call on GO.
答案1
得分: 2
JSON包在JSON文本中报告了语法错误。要找到错误的字节偏移量,请将错误断言为*json.SyntaxError,并检查Offset字段:
if e, ok := err.(*json.SyntaxError); ok {
fmt.Printf("%v: %s <<--ERROR %s\n", e, body[:e.Offset], body[e.Offset:])
}
关于发生了什么的猜测如下:返回给Go程序的响应体是XML而不是JSON。由于程序没有设置请求的接受头部、查询参数或文件扩展名来请求JSON响应,所以程序没有得到预期的JSON响应类型。
英文:
The JSON package is reporting a syntax error in the JSON text. To find the byte offset of the error, type assert the error to *json.SyntaxError and examine the Offset field:
if e, ok := err.(*json.SyntaxError); ok {
fmt.Printf("%v: %s <<--ERROR %s\n", e, body[:e.Offset], body[e.Offset:])
}
Here's a wild guess about what's going on: The response body returned to the Go program is XML, not JSON. The program is not getting the expected JSON response type because the program is not setting the request accept header, query parameter or file extension to request a JSON response.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论