drzrdc1766788 2016-02-17 18:48
浏览 72

Golang使用嵌入式XML解析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\"?>
<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": " }]

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条回答 默认 最新

  • dtjxhf595733 2016-02-17 19:23
    关注

    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
    ", 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.

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本