How to decode JSON into structures

I am trying to decode some JSON in Go, but some fields are not decoded. See the code that runs in the browser here :

What am I doing wrong?

I only need MX records, so I did not define other fields. As I understand it from a year old, you do not need to define fields that you do not use / do not need.

// You can edit this code!
// Click here and start typing.
package main

import "fmt"
import "encoding/json"

func main() {

    body := '
  {"response": {
  "status": "SUCCESS",
  "data": {
    "mxRecords": [
      {
        "value": "us2.mx3.mailhostbox.com.",
        "ttl": 1,
        "priority": 100,
        "hostName": "@"
      },
      {
        "value": "us2.mx1.mailhostbox.com.",
        "ttl": 1,
        "priority": 100,
        "hostName": "@"
      },
      {
        "value": "us2.mx2.mailhostbox.com.",
        "ttl": 1,
        "priority": 100,
        "hostName": "@"
      }
    ],
    "cnameRecords": [
      {
        "aliasHost": "pop.a.co.uk.",
        "canonicalHost": "us2.pop.mailhostbox.com."
      },
      {
        "aliasHost": "webmail.a.co.uk.",
        "canonicalHost": "us2.webmail.mailhostbox.com."
      },
      {
        "aliasHost": "smtp.a.co.uk.",
        "canonicalHost": "us2.smtp.mailhostbox.com."
      },
      {
        "aliasHost": "imap.a.co.uk.",
        "canonicalHost": "us2.imap.mailhostbox.com."
      }
    ],
    "dkimTxtRecord": {
      "domainname": "20a19._domainkey.a.co.uk",
      "value": "\"v=DKIM1; g=*; k=rsa; p=DkfbhO8Oyy0E1WyUWwIDAQAB\"",
      "ttl": 1
    },
    "spfTxtRecord": {
      "domainname": "a.co.uk",
      "value": "\"v=spf1 redirect=_spf.mailhostbox.com\"",
      "ttl": 1
    },
    "loginUrl": "us2.cp.mailhostbox.com"
  }
}}'

    type MxRecords struct {
        value    string
        ttl      int
        priority int
        hostName string
    }



    type Data struct {
        mxRecords []MxRecords
    }

    type Response struct {
        Status string 'json:"status"'
        Data   Data   'json:"data"'
    }
    type apiR struct {
        Response Response
    }

    var r apiR
    err := json.Unmarshal([]byte(body), &r)
    if err != nil {
        fmt.Printf("err was %v", err)
    }
    fmt.Printf("decoded is %v", r)

}
+8
source share
3 answers

According to the json.Unmarshal doc, you can only decode exported fields, the main reason is that external packets (such as encoding/json) cannot join unexcited fields.

json go , json , json key struct.

Exemple:

package main

import (
    "fmt"
    "encoding/json"
)

type T struct {
    Foo string `json:"foo"`
}

func main() {
    text := []byte(`{"foo":"bar"}`)
    var t T
    err := json.Unmarshal(text, &t)
    if err != nil {
        panic(err)
    }
    fmt.Println(t)
}
+18

:

type MxRecords struct {
    Value    string 'json:"value"'
    Ttl      int    'json:"ttl"'
    Priority int    'json:"priority"'
    HostName string 'json:"hostName"'
}

type Data struct {
    MxRecords []MxRecords 'json:"mxRecords"'
}

http://play.golang.org/p/EEyiISdoaE

+7

A packet encoding/jsoncan only decode into exported structure fields. Your member is Data.mxRecordsnot exported, so it is ignored when decoding. If you rename it to capitalize, the JSON package will notice it.

You will need to do the same for all members of your type MxRecords.

+1
source

Source: https://habr.com/ru/post/1545173/


All Articles