在golang中将数组插入json对象

时间:2018-03-18 06:13:08

标签: json go

我正在使用go处理文本文件中的数据,并希望输出这样的json记录:

[{
        "ship": "RMS Titanic",
        "crew": [{
            "name": "Captain Smith"
        }, {
            "name": "First Officer Murdoch"
        }],
        "passengers": [{
            "name": "Jack Dawson"
        }, {
            "name": "Rose Dewitt Bukater"
        }]
    },
    {
        "ship": "ship2",
        "crew": [{
            "name": "crew 1"
        }, {
            "name": "crew 2"
        }],
        "passengers": [{
            "name": "passenger 1"
        }, {
            "name": "passenger 2"
        }]
    }
]

以下是我的代码中的代码段:

package main

import (
        "fmt"
        "encoding/json"
)

func main() {
        var crew []map[string]string
        var passengers []map[string]string

        s1 := map[string]string{ "name": "RMS Titanic"}
        j1, _ := json.Marshal(s1)
        fmt.Printf("j1: %s\n", string(j1))

        s2 := map[string]string{ "name": "Captain Smith" }
        crew = append(crew, s2)
        s2 = map[string]string{ "name": "First Officer Murdoch" }
        crew = append(crew, s2)
        j2, _ := json.Marshal(crew)
        fmt.Printf("j2: %s\n", string(j2))

        s3 := map[string]string{ "name": "Jack Dawson"}
        passengers = append(passengers, s3)
        s3 = map[string]string{ "name": "Rose Dewitt Bukater" }
        passengers = append(passengers, s3)

        j3, _ := json.Marshal(passengers)
        fmt.Printf("j3: %s\n", string(j3))

        s4 :=  map[string]string{"crew": string(j2), "passengers": string(j3)}
        j4, _ := json.Marshal(s4)
        fmt.Printf("j4: %s\n", string(j4))

}

输出:

j1: {"name":"RMS Titanic"}
j2: [{"name":"Captain Smith"},{"name":"First Officer Murdoch"}]
j3: [{"name":"Jack Dawson"},{"name":"Rose Dewitt Bukater"}]
j4: {"crew":"[{\"name\":\"Captain Smith\"},{\"name\":\"First Officer Murdoch\"}]","passengers":"[{\"name\":\"Jack Dawson\"},{\"name\":\"Rose Dewitt Bukater\"}]"}

我正在处理 j1 中的船舶数据, j2 中的船员数据以及 j3 中的乘客数据。

我已成功将 j2 j3 合并到 j4 中,但引号s被转义,如何取消引用标记?

如何在那里插入 j1 数据,以便输出与我希望的json输出相匹配?

1 个答案:

答案 0 :(得分:1)

解决方案不是取消字符串,而是编组要序列化为JSON的完整结构,例如:

ship1 := map[string]interface{}{
    "ship": "RMS Titanic",
    "crew": crew,
    "passengers": passengers,
}

ship1Json, err := json.Marshal(ship1)
if err != nil {
    panic(err)
}

fmt.Println("ship1:", string(ship1Json))

另一个在切片中有两艘船的例子:

ship2 := map[string]interface{}{
    "ship": "ship2",
    "crew": crew,
    "passengers": passengers,
}

ships := []map[string]interface{}{ship1, ship2}

shipsJson, err := json.Marshal(ships)
if err != nil {
    panic(err)
}

fmt.Println("ships:", string(shipsJson))

如果我们打印缩进的JSON,结果会更容易看到:

indented, err := json.MarshalIndent(ships, "", "  ")
if err != nil {
    panic(err)
}

fmt.Println(string(indented))

,并提供:

[
  {
    "crew": [
      {
        "name": "Captain Smith"
      },
      {
        "name": "First Officer Murdoch"
      }
    ],
    "passengers": [
      {
        "name": "Jack Dawson"
      },
      {
        "name": "Rose Dewitt Bukater"
      }
    ],
    "ship": "RMS Titanic"
  },
  {
    "crew": [
      {
        "name": "Captain Smith"
      },
      {
        "name": "First Officer Murdoch"
      }
    ],
    "passengers": [
      {
        "name": "Jack Dawson"
      },
      {
        "name": "Rose Dewitt Bukater"
      }
    ],
    "ship": "ship2"
  }
]

另见playground.