dt614037527
dt614037527
2017-02-21 22:03

map [string]结构体中的结构体

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duanlinzhen7235 duanlinzhen7235 4年前

    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).

    点赞 评论 复制链接分享
  • dpzbh1779 dpzbh1779 4年前

    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.

    点赞 评论 复制链接分享