简体   繁体   中英

map[string] struct inside struct

I have a JSON file that looks like this:

{
  "jailbreaks": [
    {
      "jailbroken": false,
      "name": "",
      "version": "",
      "url": "",
      "anleitung": [],
      "ios": {
        "start": "10.2.1"
      },
      "caveats": "",
      "platforms": []
    },
    {
      "jailbroken": true,
      "name": "Yalu102",
      "version": "beta 6",
      "url": "https://domain-dl.tld",
      "anleitung": [
        { "blog": "title", "link": "http://domain.tld/" },
        { "blog": "Test", "link": "http://google.at" }
      ],
      "ios": {
        "start": "10.2"
      },
      "caveats": "some text here",
      "platforms": [
        "Windows",
        "OS X",
        "Linux"
      ]
    },

And I create the object to work with like this:

type Jailbreak struct {
    Jailbroken bool   `json:"jailbroken"`
    Name       string `json:"name"`
    Version    string `json:"version"`
    URL        string `json:"url"`
    Anleitung  map[string]struct {
        Name string `json:"blog"`
        Link string `json:"link"`
    } `json:"anleitung"`

    Firmwares struct {
        Start string `json:"start"`
        End   string `json:"end"`
    } `json:"ios"`

    Platforms []string `json:"platforms"`
    Caveats   string   `json:"caveats"`
}

When I want to build my go program I get an error, that the JSON file cannot be read. But as soon as I delete the map[string]struct I can compile and run the program without any error and everything works fine. Am I messing around with something or is there an error in my JSON file?

The json provided is not valid (as the array does not have a closing ] and the top level json object lacks another closing } ) so let's assume it's like:

{
  "jailbreaks": [
    {
      "jailbroken": false,
      "name": "",
      "version": "",
      "url": "",
      "anleitung": [],
      "ios": {
        "start": "10.2.1",
        "end": ""
      },
      "platforms": [],
      "caveats": ""
    },
    {
      "jailbroken": true,
      "name": "Yalu102",
      "version": "beta 6",
      "url": "https://domain-dl.tld",
      "anleitung": [
        {
          "blog": "title",
          "link": "http://domain.tld/"
        },
        {
          "blog": "Test",
          "link": "http://google.at"
        }
      ],
      "ios": {
        "start": "10.2",
        "end": ""
      },
      "platforms": [
        "Windows",
        "OS X",
        "Linux"
      ],
      "caveats": "some text here"
    }
  ]
}

The data structure Jailbreaks (first one), marshals-to/unmarshals-from this json properly:

type Jailbreaks struct {
    List []Jailbreak `json:"jailbreaks"`
}

type Jailbreak struct {
    Jailbroken bool   `json:"jailbroken"`
    Name       string `json:"name"`
    Version    string `json:"version"`
    URL        string `json:"url"`
    Anleitung  []struct {
        Name string `json:"blog"`
        Link string `json:"link"`
    } `json:"anleitung"`

    Firmwares struct {
        Start string `json:"start"`
        End   string `json:"end"`
    } `json:"ios"`

    Platforms []string `json:"platforms"`
    Caveats   string   `json:"caveats"`
}

As you see Anleitung is declared as a slice (not a map).

Use omitempty flag for when your "anleitung" is empty in JSON to be consumed. Beware though, when that is the case, your Jailbreak struct won't have an "anleitung" field.

Change your map's json flag to to;

Anleitung   map[string]struct {
    Name string `json:"blog"`
    Link string `json:"link"`
} `json:"anleitung,omitempty"`

Option 2;

I guess you could also use Anleitung map[string]interface{} but that is better for "holding a map of strings to arbitrary data types". In your case the data is not arbitrary but rather, empty I guess. And looks like that is just temporary.

I'd go for option 1, then I'd check if my struct contains any Anleitung data or not before accessing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM