gpt4 book ai didi

json - Golang 解析带有嵌入式 XML 的 JSON

转载 作者:数据小太阳 更新时间:2023-10-29 03:33:18 27 4
gpt4 key购买 nike

我有一个 JSON 格式的 http 响应主体,但它包含一个字段,这是一个作为字符串的 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"`
}

我收到“无效字符‘<’正在寻找值的开头”错误

我也尝试过使用字符串映射,但仍然出现同样的错误。

json响应的开头是:

[{
"ccda": "<?xml version=\"1.0\"?>\n<ClinicalDocument xmlns=\"urn:hl7-org:v3\"..."
}]

ccda 是json 字符串中的唯一元素。同样,我不想解析 XML。

GO 处理转义引号的方式有问题吗? json元素ccda的值为XML字符串。

查看来自 vendor 网站(他们的工具)的原始数据,我得到了这个:

[{ "ccda": "\n\n\n\n\n\n\n\n }]

当我读取 response.Body 并转换为字符串时,我得到了这个(这是不正确的,尽管我可以使用它):

<?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/site 工具上取回正确的 JSON。只是不在 GO 的这个电话上。

最佳答案

JSON 包报告 JSON 文本中存在语法错误。要查找错误的字节偏移量,请键入 assert the error to *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 响应类型,因为该程序未设置请求接受 header 、查询参数或文件扩展名来请求 JSON 响应。

关于json - Golang 解析带有嵌入式 XML 的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35465152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com