Go 将整型片封送到字符串行

Go 将整型片封送到字符串行,go,Go,我有这样一个结构: type Int64Slice []int64 type DataWrapper struct { ListId Int64Slice `json:"listid" required` Domain string `json:"domain" required` Name string `json:"name,omitempty"` } 我需要它变成: { "li

我有这样一个结构:

type Int64Slice []int64
type DataWrapper struct {
    ListId Int64Slice    `json:"listid" required`
    Domain string `json:"domain" required`
    Name   string `json:"name,omitempty"`
}
我需要它变成:

{
    "listid": "1 2 3 4 5",
    "domain": "mydomain"
}
我已经编写了自定义MarshallJSON:

func (u Int64Slice) MarshalJSON() ([]byte, error) {
    var result string
    if u == nil {
        result = "null"
    } else {
        result = strings.Trim(strings.Join(strings.Fields(fmt.Sprint(u)), " "), "[]")
        Logger.Debugln(result)
    }
    return []byte(result), nil
}

func (d *DataWrapper) ToJSON() []byte {
    result, err := json.Marshal(d)
    if err != nil {
        log.Fatalln(err)
        panic(err)
    }
    return result
}
在第
Logger.Debugln(result)
行,它将打印此结果:

20170830090317506 20170830090026319 20170830111023194 201708301043081

json:调用类型模型的MarshalJSON时出错。Int64Slice:无效 顶级值后的字符“2”


我想你把它倒过来了。 使用
bytes.Buffer
类型以增量方式构建数据的字符串表示形式

节目

package main

import (
    "bytes"
    "encoding/json"
    "os"
    "strconv"
)

type Int64Slice []int64

func (s Int64Slice) MarshalJSON() ([]byte, error) {
    if s == nil {
        return []byte("null"), nil
    }

    var b bytes.Buffer
    b.WriteByte('"')
    for i, v := range s {
        if i > 0 {
            b.WriteByte('\x20')
        }
        b.WriteString(strconv.FormatInt(v, 10))
    }

    b.WriteByte('"')
    return b.Bytes(), nil
}

func main() {
    var (
        a Int64Slice = nil
        b = Int64Slice{
            42,
            12,
            0,
        }
    )
    enc := json.NewEncoder(os.Stdout)
    enc.Encode(a)
    enc.Encode(b)
}
印刷品:

null
"42 12 0"

.

我想你把它倒过来了。 使用
bytes.Buffer
类型以增量方式构建数据的字符串表示形式

节目

package main

import (
    "bytes"
    "encoding/json"
    "os"
    "strconv"
)

type Int64Slice []int64

func (s Int64Slice) MarshalJSON() ([]byte, error) {
    if s == nil {
        return []byte("null"), nil
    }

    var b bytes.Buffer
    b.WriteByte('"')
    for i, v := range s {
        if i > 0 {
            b.WriteByte('\x20')
        }
        b.WriteString(strconv.FormatInt(v, 10))
    }

    b.WriteByte('"')
    return b.Bytes(), nil
}

func main() {
    var (
        a Int64Slice = nil
        b = Int64Slice{
            42,
            12,
            0,
        }
    )
    enc := json.NewEncoder(os.Stdout)
    enc.Encode(a)
    enc.Encode(b)
}
印刷品:

null
"42 12 0"

.20170830090317506 20170830090026319 20170830111023194 201708301043081不是有效的JSON值。它被解释为一个有效数字(
20170830090317506
),后跟一个有效空格,后跟无效数据,以
2
字符开头;因此,你观察到的错误

需要在其周围加上引号:

尝试以下方法:

result = `"` + strings.Trim(strings.Join(strings.Fields(fmt.Sprint(u)), " "), "[]") + `"`

20170830090317506 20170830090026319 20170830111023194 201708301043081
不是有效的JSON值。它被解释为一个有效数字(
20170830090317506
),后跟一个有效空格,后跟无效数据,以
2
字符开头;因此,你观察到的错误

需要在其周围加上引号:

尝试以下方法:

result = `"` + strings.Trim(strings.Join(strings.Fields(fmt.Sprint(u)), " "), "[]") + `"`

您需要将数字括在双引号中。是否有特殊原因不使用切片?在JSOn中,某些东西的列表应该表示为数组。@MarkusWMahlberg这是其他人的设计,我必须遵循it@mkopriva哦,我的上帝。该死的。谢谢你你需要把数字用双引号括起来。有没有特别的理由不使用切片?在JSOn中,某些东西的列表应该表示为数组。@MarkusWMahlberg这是其他人的设计,我必须遵循it@mkopriva哦,我的上帝。该死的。谢谢很多答案都指出了问题的原因-双引号。我会选择这个最好的,因为性能有所提高。谢谢大家btwmany的回答已经指出了问题的原因-双引号。我会选择这个最好的,因为性能有所提高。谢谢大家