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.

    评论

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形