简体   繁体   English

JSON.stringify 在 Golang 中等效于 map[string]interface{}

[英]JSON.stringify equivalent in Golang for map[string]interface{}

I'm new to Golang.我是 Golang 的新手。 I have to stringify a map[string]interface{} in Golang as you would do a JSON.stringify in javascript.我必须在 Golang 中对map[string]interface{}进行字符串化, JSON.stringify在 javascript 中执行 JSON.stringify 一样。 In javascript keys which has their values undefined gets omitted when you do JSON.stringify and I want the same thing when I do json.Marshal in Golang for nil values.在JavaScript的按键,有自己的价值观undefined ,当你做被省略JSON.stringify ,我想同样的事情,当我做json.Marshal在Golang nil值。 For eg in javascript例如在javascript中

var activity = {"isvalid": true, "value": {"prop": undefined}, "prop": undefined}
console.log(JSON.stringify(activity)) 
// Output: {"isvalid":true,"value":{}}

But in Golang if I do this但是在 Golang 如果我这样做

activity := map[string]interface{}{
    "isvalid": true, 
    "value": map[string]interface{}{"prop": nil},
    "prop": nil,
}
jsonStr, _ := json.Marshal(activity)
fmt.Println(string(jsonStr)) 
// output: {"isvalid":true,"prop":null,"value":{"prop":null}}

https://play.golang.org/p/dvmz32phdNU https://play.golang.org/p/dvmz32phdNU

There is a difference in output, what is a good way to make the output same in Golang as of Javascript?输出有所不同,在 Golang 中使输出与 Javascript 相同的好方法是什么?

EDIT:编辑:

The use case involves getting data from various sources which are of type map[string]interface{} and keys can be arbitrary, so converting it into a struct is not an option for me.用例涉及从map[string]interface{}类型的各种来源获取数据,并且键可以是任意的,因此将其转换为 struct 对我来说不是一个选择。

https://play.golang.org/p/KeeU7eagWh6 https://play.golang.org/p/KeeU7eagWh6

Define your variable as a struct and mark how each field should be marshalled into a json.将变量定义为结构体并标记每个字段应如何编组到 json 中。 There is an omit tag that can be used to ignore nil fields.有一个 omit 标签可用于忽略 nil 字段。 Refer to the example provided参考提供的例子

Since OP has mentioned he cannot use structs, one other way is to recursively go through the map and remove entries where the value is nil.由于 OP 已经提到他不能使用结构,另一种方法是递归遍历映射并删除值为 nil 的条目。

https://play.golang.org/p/PWd9fpWrKZH https://play.golang.org/p/PWd9fpWrKZH

package main

import (
    "encoding/json"
    "fmt"
)


func main() {
    data := map[string]interface{}{
        "isvalid": true, 
        "value": map[string]interface{}{"prop": nil},
        "prop": nil,
    }    
    final := removeNils(data)
    out, err := json.Marshal(final)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(out))
}

func removeNils(initialMap map[string]interface{}) map[string]interface{} {
    withoutNils := map[string]interface{}{}
    for key, value := range initialMap {
        _, ok := value.(map[string]interface{})
        if ok {
            value = removeNils(value.(map[string]interface{}))
            withoutNils[key] = value
            continue
        }
        if value != nil {
            withoutNils[key] = value
        }
    }
    return withoutNils
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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