douhe6181 2016-08-04 12:52
浏览 328
已采纳

golang无法解析json项目

I can't use golang to parse this json with server response:

{
"id": 1,
"description": "{\"uDes\":\"\",\"reDes\":[{\"des\":\"aaa,,request=<?xml version=“1.0“ encoding=“GBK“?><Req ver=“3.0“><HR version=“3.0“ cd=“0“><src><\\\\/src><license><\\\\/license><clientid>com.sample.xo<\\\\/clientid><ii>8603<\\\\/ii><im>46002910<\\\\/im><uuid>da032b2<\\\\/uuid><reqid>-21851<\\\\/reqid><smc>44:91<\\\\/smc><sdv>2.0.201501131131.a.r<\\\\/sdv><corv>V1.1.0r<\\\\/corv><\\\\/HDR><DRR nettype=“LTE“ inftype=“1“><mc>460<\\\\/cc><mnc>00<\\\\/mnc><lac>34809<\\\\/lac><cellid>8842241<\\\\/cellid><signal>-69<\\\\/signal><\\\\/DRR><\\\\/Cell_Req>&retype=3&timestamp=1469550331136\"}]}"
}

please note the key 'description' has a lot of illegal character.I just want to get and store it as a string into our database. the go code fragment following:

import (
   "fmt"
   "net/http"
   "encoding/json"
)
var p := fmt.Println
type DataSlice struct{
     Id int
     Destription string
}
func main() {
    p("Start fetching data..")
    resp,err := http.Get("http://127.0.0.1/pis")
    if err != nil{
    //handle error
       p(err)
    }else{
       defer resp.Body.Close()
       var jsonInst DataSlice
       p("Start decode json body..")
       err := json.NewDecoder(resp.Body).Decode(&jsonInst)
       if err != nil {
         panic(err)
       }else{
         //do insert here
       }
    }
}

this code execute fine,but the description value is empty.please help me to resolve this problem. Thanks a lot!

  • 写回答

1条回答 默认 最新

  • druybew06513 2016-08-04 13:10
    关注

    Notice that you have a typo - Destription instead of Description in your DataSlice struct. See the following example in PlayGround that successfully parses your JSON blob - https://play.golang.org/p/CeoCH2SJXh:

    package main
    
    import (
        "encoding/json"
        "fmt"
        "strings"
    )
    
    var input = `{
    "id": 1,
    "description": "{\"uDes\":\"\",\"reDes\":[{\"des\":\"aaa,,request=<?xml version=“1.0“ encoding=“GBK“?><Req ver=“3.0“><HR version=“3.0“ cd=“0“><src><\\\\/src><license><\\\\/license><clientid>com.sample.xo<\\\\/clientid><ii>8603<\\\\/ii><im>46002910<\\\\/im><uuid>da032b2<\\\\/uuid><reqid>-21851<\\\\/reqid><smc>44:91<\\\\/smc><sdv>2.0.201501131131.a.r<\\\\/sdv><corv>V1.1.0r<\\\\/corv><\\\\/HDR><DRR nettype=“LTE“ inftype=“1“><mc>460<\\\\/cc><mnc>00<\\\\/mnc><lac>34809<\\\\/lac><cellid>8842241<\\\\/cellid><signal>-69<\\\\/signal><\\\\/DRR><\\\\/Cell_Req>&retype=3&timestamp=1469550331136\"}]}"
    }`
    
    var p = fmt.Println
    
    type DataSlice struct {
        Id          int
        Description string
    }
    
    func main() {
        var inst DataSlice
        p("Start decode json body..")
        err := json.NewDecoder(strings.NewReader(input)).Decode(&inst)
        if err != nil {
            panic(err)
        }
        fmt.Printf("%#v
    ", inst)
    }
    

    Output:

    Start decode json body..
    
    main.DataSlice{Id:1, Description:"{\"uDes\":\"\",\"reDes\":[{\"des\":\"aaa,,request=<?xml version=“1.0“ encoding=“GBK“?><Req ver=“3.0“><HR version=“3.0“ cd=“0“><src><\\\\/src><license><\\\\/license><clientid>com.sample.xo<\\\\/clientid><ii>8603<\\\\/ii><im>46002910<\\\\/im><uuid>da032b2<\\\\/uuid><reqid>-21851<\\\\/reqid><smc>44:91<\\\\/smc><sdv>2.0.201501131131.a.r<\\\\/sdv><corv>V1.1.0r<\\\\/corv><\\\\/HDR><DRR nettype=“LTE“ inftype=“1“><mc>460<\\\\/cc><mnc>00<\\\\/mnc><lac>34809<\\\\/lac><cellid>8842241<\\\\/cellid><signal>-69<\\\\/signal><\\\\/DRR><\\\\/Cell_Req>&retype=3&timestamp=1469550331136\"}]}"}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog