Parsing JSON in Go

I am new to Go and I am creating a web API that uses data from RDW in my android app. However, I am unable to parse JSON in Go where, as in Java, I could easily use for example: jsonObject.getString ("AreaId"), it looks like I cannot do this in Go.

I am trying to parse the following JSON to store it in a MySQL db:

{
  '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 the markup. What am I doing wrong? What needs to be done to take care of this?

+3


source to share


1 answer


You need to use JSON annotations (called structural "tags" in docs) to tell him that the field name is in JSON:

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

      

I believe Go takes care of results

vs automatically results

, but if you're going to send data back and want it to be lowercase, you still need the annotation.

Yours is Results Result

wrong - the results are not an object of type Result, it is an AreaGeometry array, so it should look like this:



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, which is in 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

+6


source







All Articles