douxin1163 2014-08-14 16:16
浏览 47
已采纳

在Go中解析JSON

I am new to Go, and I am building a web API that uses data from the RDW in my Android Application. However I have trouble parsing JSON in Go, where as in Java I could easily use something like jsonObject.getString("AreaId"). It does not seem like I can do this in Go.

I am trying to parse the following JSON to save it in a MySQL database:

{
  'd': {
    'results': [
      {
        '__metadata': {
          'id': 'https://api.datamarket.azure.com/Data.ashx/opendata.rdw/StatischParkeren.Open.Data/v1/AREAGEOMETRY(1)',
          'uri': 'https://api.datamarket.azure.com/Data.ashx/opendata.rdw/StatischParkeren.Open.Data/v1/AREAGEOMETRY(1)',
          'type': 'opendata.rdw.StatischParkeren.Open.Data.AREAGEOMETRY'
        },
        'AREAGEOMETRY_ID': 1,
        'AreaManagerId': '34',
        'AreaId': 'ALMBUITEN',
        'EndDateArea': null,
        'StartDateArea': '/Date(1330560000000)/',
        'AreaGeometryTxt': 'POLYGON ((5.2736760005354881 52.391216600313783, 5.2752534225583076 52.392331821843982, 5.2771425843238831 52.391448376700282, 5.2795117920336709 52.392518069183787, 5.2794937640428543 52.392529543489218, 5.2795250192284584 52.392524041235447, 5.2795117920336709 52.392518069183787, 5.27959094196558 52.392467692494392, 5.28429589420557 52.394528595730662, 5.2830820381641388 52.3958012573421, 5.2826158106327057 52.396464187651873, 5.28216527402401 52.396755328401923, 5.2811877280473709 52.396325327456, 5.27938649058342 52.395481485873461, 5.2793451324105263 52.395514352247119, 5.2788525596261024 52.395813956856728, 5.278492659330368 52.39611235447228, 5.2776647135615349 52.396820867434144, 5.2777034044265747 52.396853528916836, 5.2768759876489639 52.397581102326512, 5.274177111685276 52.396415254101157, 5.2741994857788086 52.396424867212772, 5.2686141580343246 52.394008679315448, 5.26860549300909 52.394005045294762, 5.2691666409373283 52.39350445382297, 5.2705547362565994 52.392721636220813, 5.2723518460904533 52.392156438032842, 5.2723518460989 52.39215643890202, 5.272359311580658 52.392154090106487, 5.2723518460904533 52.392156438032842, 5.2723498195409775 52.391947904601693, 5.273051455616951 52.391403153538704, 5.2736760005354881 52.391216600313783))',
        'AreaGeometryGml': '<Polygon xmlns="http://www.opengis.net/gml"><exterior><LinearRing><posList>5.2736760005354881 52.391216600313783 5.2752534225583076 52.392331821843982 5.2771425843238831 52.391448376700282 5.2795117920336709 52.392518069183787 5.2794937640428543 52.392529543489218 5.2795250192284584 52.392524041235447 5.2795117920336709 52.392518069183787 5.27959094196558 52.392467692494392 5.28429589420557 52.394528595730662 5.2830820381641388 52.3958012573421 5.2826158106327057 52.396464187651873 5.28216527402401 52.396755328401923 5.2811877280473709 52.396325327456 5.27938649058342 52.395481485873461 5.2793451324105263 52.395514352247119 5.2788525596261024 52.395813956856728 5.278492659330368 52.39611235447228 5.2776647135615349 52.396820867434144 5.2777034044265747 52.396853528916836 5.2768759876489639 52.397581102326512 5.274177111685276 52.396415254101157 5.2741994857788086 52.396424867212772 5.2686141580343246 52.394008679315448 5.26860549300909 52.394005045294762 5.2691666409373283 52.39350445382297 5.2705547362565994 52.392721636220813 5.2723518460904533 52.392156438032842 5.2723518460989 52.39215643890202 5.272359311580658 52.392154090106487 5.2723518460904533 52.392156438032842 5.2723498195409775 52.391947904601693 5.273051455616951 52.391403153538704 5.2736760005354881 52.391216600313783</posList></LinearRing></exterior></Polygon>'
      },
      {
        '__metadata': {
          'id': 'https://api.datamarket.azure.com/Data.ashx/opendata.rdw/StatischParkeren.Open.Data/v1/AREAGEOMETRY(2)',
          'uri': 'https://api.datamarket.azure.com/Data.ashx/opendata.rdw/StatischParkeren.Open.Data/v1/AREAGEOMETRY(2)',
          'type': 'opendata.rdw.StatischParkeren.Open.Data.AREAGEOMETRY'
        },
        'AREAGEOMETRY_ID': 2,
        'AreaManagerId': '34',
        'AreaId': 'ALMTEGELZ',
        'EndDateArea': null,
        'StartDateArea': '/Date(1330560000000)/',
        'AreaGeometryTxt': 'POLYGON ((5.216308925 52.370733155, 5.216800447 52.370804276, 5.216837124 52.370755791, 5.217178542 52.370807152, 5.217373999 52.370322534, 5.217353484 52.370319431, 5.217371539 52.370274734, 5.216664547 52.370167441, 5.216571837 52.370396589, 5.216451509 52.370380301, 5.216308925 52.370733155))',
        'AreaGeometryGml': '<Polygon xmlns="http://www.opengis.net/gml"><exterior><LinearRing><posList>5.216308925 52.370733155 5.216800447 52.370804276 5.216837124 52.370755791 5.217178542 52.370807152 5.217373999 52.370322534 5.217353484 52.370319431 5.217371539 52.370274734 5.216664547 52.370167441 5.216571837 52.370396589 5.216451509 52.370380301 5.216308925 52.370733155</posList></LinearRing></exterior></Polygon>'
      },
 ],
    '__next': 'https://api.datamarket.azure.com/Data.ashx/opendata.rdw/StatischParkeren.Open.Data/v1/AREAGEOMETRY?$skiptoken=100'
  }
}

This is my code in Go:

type Data struct {
    Next string
    Results Result
}

type Result struct {
    Results []AreaGeometry
}

type AreaGeometry struct {
    AREAGEOMETRY_ID int32
    AreaManagerId int64
    AreaId string
    EndDateArea string
    StartDateArea string
    AreaGeometryTxt string
}

// Synchronizes the AreaGeometry in our MySQL database with the AreaGeometry in the database from the RDW
func retrieveData() {
    resp, err := http.Get("https://api.datamarket.azure.com/opendata.rdw/StatischParkeren.Open.Data/v1/AREAGEOMETRY?$format=json")
    if err != nil {
        panic(err.Error()) // TODO: proper error handling
    }
    body, err := ioutil.ReadAll(resp.Body)
    var d Data
    json.Unmarshal(body, &d)
    fmt.Println("Next: " + d.Next)
}

However, d.Next returns an empty string after unmarshaling. What am I doing wrong? What is the Go way of taking care of this?

  • 写回答

1条回答 默认 最新

  • dongyou7472 2014-08-14 16:23
    关注

    You need to use JSON annotations (referred to as struct "tags" in the docs) to let it know what the name of the field is in the JSON:

    type Data struct {
        Next string `json:"__next"` //Field appears in JSON as key "__next".
        Results Result `json:"results"`
    }
    

    I believe Go automatically takes care of results vs Results, but if you are going to send data back and want it to be lowercase, you still need the annotation.

    Your Results Result is incorrect - Results is not an object of type Result, it is an array of AreaGeometry, so it should look like:

    type Data struct {
        Next string  `json:"__next"` //Field appears in JSON as key "__next".
        Results []AreaGeometry
    }
    

    Also, you need a wrapper for the D object that is in the JSON:

    type D struct {
        D Data `json:"d"`
    }
    
    
    func main() {
        body := jsonString //too long to put here, but in the playground. 
        var d D
        err := json.Unmarshal([]byte(body), &d)
        if (err != nil) {
           fmt.Println(err)
        }
        fmt.Println("Next: " + d.D.Next)
    
    }
    

    http://play.golang.org/p/yK-zNxEMvy

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名