dshkmamau65777662 2018-03-18 06:13
浏览 597
已采纳

在golang中将数组插入json对象

I am processing data from a text file with go and would like to ouput a json record like this:

[{
        "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"
        }]
    }
]

Here is a snippet from my code:

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
", 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
", 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
", string(j3))

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

}

Output:

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\"}]"}

I am processing the ship data in j1, the crew data in j2 and the passengers data in j3.

I have managed to merge j2 and j3 together into j4, but the quotation mark s are escapaded, how un-escape the quotation marks ?

How to insert j1 data in there so the output match the json output I wish for ?

  • 写回答

1条回答 默认 最新

  • douzhou7037 2018-03-18 06:28
    关注

    The solution is not to unescape the string, but to marshal the complete structure you want to serialize to JSON, for example:

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

    Another example with two ships in a slice:

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

    The result is easier to see if we print the JSON indented:

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

    Giving:

    [
      {
        "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"
      }
    ]
    

    See also on the playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog