将JSON反序列化为特定类型(字符串)

huangapple go评论89阅读模式
英文:

Unmarshalling JSON to certain type (string) always

问题

我有一些 JSON 数据,第一个元素可以是数字或字符串。我希望始终将其存储为字符串值,但实际上它被读取为数字类型,导致程序崩溃。

我尝试强制将其解析为字符串,但没有成功。

string `json:",string"`

我正在遵循这个指南,它似乎很适合我的数据。

如何确保始终将位于 [0] 的元素读取并保存为字符串?

以下是代码和 Playground 链接...

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

package main

import "fmt"
import "log"
import "encoding/json"

const inputWorking = `
["AAAAAA", {"testcode" : "Sss"}, 66666666]
`

const inputBroken = `
[111111, {"testcode" : "Sss"}, 66666666]
`

type RawMessage struct {
    AlwaysString   string `json:",string"`
    ClientData     ClientData
    ReceptionTime  int
}

type ClientData struct {
    testcode string
}

func main() {
    
    var n RawMessage
    if err := json.Unmarshal([]byte(inputWorking), &n); err != nil {
        log.Fatal(err)
    }
    
    fmt.Printf("%#v\n", n)
    
    var o RawMessage
    if err := json.Unmarshal([]byte(inputBroken), &o); err != nil {
        log.Fatal(err)
    }
    
    fmt.Printf("%#v\n", o)
}

func (n *RawMessage) UnmarshalJSON(buf []byte) error {
    tmp := []interface{}{&n.AlwaysString, &n.ClientData, &n.ReceptionTime}
    wantLen := len(tmp)
    if err := json.Unmarshal(buf, &tmp); err != nil {
        return err
    }
    if g, e := len(tmp), wantLen; g != e {
        return fmt.Errorf("wrong number of fields in RawMessage: %d != %d", g, e)
    }
    return nil
}
英文:

I have some JSON that can have either a number or a string as the first element. I always want to be able to store this as a string value but instead I get a crash as it is reading it as quite rightly so a number type.

I tried to force the unmarshalling as a string but this was not successful.

string `json:",string"`

I am following this guide which seems to fit my data nicely.

How can I always get this element at [0] to always read and save as a string?

Code & Playground below...

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

package main

import "fmt"
import "log"
import "encoding/json"

const inputWorking = `
["AAAAAA", {"testcode" : "Sss"}, 66666666]
`

const inputBroken = `
[111111, {"testcode" : "Sss"}, 66666666]
`

type RawMessage struct {
	AlwaysString        string `json:",string"`
	ClientData    ClientData
	ReceptionTime int
}

type ClientData struct {
	testcode string
}

func main() {
	
	var n RawMessage
	if err := json.Unmarshal([]byte(inputWorking), &n); err != nil {
		log.Fatal(err)
	}
	
	fmt.Printf("%#v\n", n)
	
	var o RawMessage
	if err := json.Unmarshal([]byte(inputBroken), &o); err != nil {
		log.Fatal(err)
	}
	
	fmt.Printf("%#v\n", o)
}

func (n *RawMessage) UnmarshalJSON(buf []byte) error {
	tmp := []interface{}{&n.AlwaysString, &n.ClientData, &n.ReceptionTime}
	wantLen := len(tmp)
	if err := json.Unmarshal(buf, &tmp); err != nil {
		return err
	}
	if g, e := len(tmp), wantLen; g != e {
		return fmt.Errorf("wrong number of fields in RawMessage: %d != %d", g, e)
	}
	return nil
}

答案1

得分: 3

你可以创建一个通用的接收器,即接口类型,并进行类型断言:

package main

import (
	"encoding/json"
	"fmt"
)

type RawMessage struct {
	UnknownType interface{} `json:"unknown_type"`
}

const inputString = `{"unknown_type" : "a"}`

const inputFloat = `{"unknown_type" : 123}` // 注意:默认情况下解组为float64类型!!!

func main() {

	var n RawMessage
	if err := json.Unmarshal([]byte(inputFloat), &n); err != nil {
		println(err.Error())
	}

	switch v := n.UnknownType.(type) {
	case string:
		fmt.Printf("接收到一个字符串:%v", v)
	case float64:
		fmt.Printf("接收到一个数字:%v", v)
	default:
		fmt.Printf("未知类型:%v", v)
	}
}
英文:

You can create a universal receiver, i.e. of interface type, and then do a type assertion:

package main

import (
	"encoding/json"
	"fmt"
)

type RawMessage struct {
	UnknownType interface{} `json:"unknown_type"`
}

const inputString = `{"unknown_type" : "a"}`

const inputFloat = `{"unknown_type" : 123}` // Note: Unmarshals into a float64 by default!!!

func main() {

	var n RawMessage
	if err := json.Unmarshal([]byte(inputFloat), &n); err != nil {
		println(err.Error())
	}

	switch v := n.UnknownType.(type) {
	case string:
		fmt.Printf("Received a string: %v", v)
	case float64:
		fmt.Printf("Received a number: %v", v)
	default:
		fmt.Printf("Unknown type: %v", v)
	}
}

答案2

得分: 3

我遇到了同样的问题,到目前为止,我找到的最好的解决办法是使用自定义的解码器将字符串转换为类型:

package main

import (
	"encoding/json"
	"fmt"
	"strconv"
)

// Stringish exists because api send back integers unquoted even if underlying type is string
type StringIsh string

func (f *StringIsh) UnmarshalJSON(data []byte) error {

	var receiver string
	if len(data) == 0 {
		return nil
	}
	if data[0] != '"' {
		quoted := strconv.Quote(string(data))
		data = []byte(quoted)
	}

	if err := json.Unmarshal(data, &receiver); err != nil {
		return err
	}
	*f = StringIsh(receiver)
	return nil

}

type Test struct {
	Foo StringIsh
}

func main() {
	dest := &Test{}
	_ = json.Unmarshal([]byte(`{"foo": "bar"}`), &dest)
	fmt.Println(dest)

	dest = &Test{}
	_ = json.Unmarshal([]byte(`{"foo": 123}`), &dest)
	fmt.Println(dest)
}

你可以在 https://goplay.space/#i0OVs9tnule 上自行尝试。

英文:

I had the same problem and so far the best I found was to make a type from string with a custom unmarshaller:

package main

import (
	"encoding/json"
	"fmt"
	"strconv"
)

// Stringish exists because api send back integers unquoted even if underlying type is string
type StringIsh string

func (f *StringIsh) UnmarshalJSON(data []byte) error {

	var receiver string
	if len(data) == 0 {
		return nil
	}
	if data[0] != '"' {
		quoted := strconv.Quote(string(data))
		data = []byte(quoted)
	}

	if err := json.Unmarshal(data, &receiver); err != nil {
		return err
	}
	*f = StringIsh(receiver)
	return nil

}

type Test struct {
	Foo StringIsh
}

func main() {
	dest := &Test{}
	_ = json.Unmarshal([]byte(`{"foo": "bar"}`), &dest)
	fmt.Println(dest)

	dest = &Test{}
	_ = json.Unmarshal([]byte(`{"foo": 123}`), &dest)
	fmt.Println(dest)
}

Try it yourself at https://goplay.space/#i0OVs9tnule

答案3

得分: -1

尝试这个方法,将用户输入定义为通用接口:

interface{} `json:",string"`

package main

import "fmt"
import "log"
import "encoding/json"

const inputWorking = `
["AAAAAA", {"testcode" : "Sss"}, 66666666]
`

const inputBroken = `
[111111, {"testcode" : "Sss"}, 66666666]
`

type RawMessage struct {
    AlwaysString  interface{} `json:",string"`
    ClientData    ClientData
    ReceptionTime int
}

type ClientData struct {
    testcode string
}

func main() {

    var n RawMessage
    if err := json.Unmarshal([]byte(inputWorking), &n); err != nil {
        log.Fatal(err)
    }

    fmt.Printf("%#v\n", n)

    var o RawMessage
    if err := json.Unmarshal([]byte(inputBroken), &o); err != nil {
        log.Fatal(err)
    }
}

以上是代码的翻译。

英文:

Try this if it resolve this issue, make a generic interface as user input is not defined

interface{} `json:",string"`

package main

import "fmt"
import "log"
import "encoding/json"

const inputWorking = `
["AAAAAA", {"testcode" : "Sss"}, 66666666]
`

const inputBroken = `
[111111, {"testcode" : "Sss"}, 66666666]
`

type RawMessage struct {
    AlwaysString        interface{} `json:",string"`
    ClientData    ClientData
    ReceptionTime int
}

type ClientData struct {
    testcode string
}

func main() {

    var n RawMessage
    if err := json.Unmarshal([]byte(inputWorking), &n); err != nil {
        log.Fatal(err)
    }

    fmt.Printf("%#v\n", n)

    var o RawMessage
    if err := json.Unmarshal([]byte(inputBroken), &o); err != nil {
        log.Fatal(err)
    }

huangapple
  • 本文由 发表于 2016年12月2日 22:08:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/40934108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定