【问题标题】:How to convert complex json string with nested objects to map in golang?如何将带有嵌套对象的复杂json字符串转换为golang中的映射?
【发布时间】:2018-01-02 05:31:58
【问题描述】:

我有一个 json 格式的复杂字符串,我想将其转换为 golang 中的地图。假设字符串是

      species :{
               "type" : "human"
               "age" : "23"
               "attributes" : {
                              "height" : "182"
                              "weight" : "160"
                              "contact" : {
                                          "address" : ########
                                          "phone" : #########
                              }
               }
      }

我如何解析它使得 map[attributes] 又是一个 map[string] 接口等等?

【问题讨论】:

  • 看看encoding/json是怎么做的。

标签: dictionary go


【解决方案1】:

您可以使用 map[string]interface{},例如:

species := make(map[string]interface{})
if err := json.Unmarshal([]byte(jsonStr), &species); err != nil {
   // deal with error
}

【讨论】:

    猜你喜欢
    • 2019-11-24
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多