在Golang中解析转义的JSON字符串

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

Parse escaped json string in Golang

问题

你好!要将上述的 JSON 字符串转换为结构体,你可以使用编程语言提供的 JSON 解析库来实现。以下是一个示例代码,展示了如何在 Python 中完成这个任务:

import json

json_string = '{"A":"a","B":"b","C":"c","D":2,"E":"e"}'
data = json.loads(json_string)

class MyStruct:
    def __init__(self, A, B, C, D, E):
        self.A = A
        self.B = B
        self.C = C
        self.D = D
        self.E = E

my_struct = MyStruct(data['A'], data['B'], data['C'], data['D'], data['E'])

在这个示例中,我们首先使用 json.loads() 函数将 JSON 字符串解析为 Python 字典对象。然后,我们定义了一个名为 MyStruct 的类,它具有与 JSON 字符串中的键对应的属性。最后,我们使用解析后的数据创建了一个 MyStruct 对象。

请注意,这只是一个示例,你可以根据自己的需求进行修改和扩展。希望对你有帮助!如果你使用的是其他编程语言,可以告诉我,我可以为你提供相应的示例代码。

英文:

Say I have this json string:

{\"A\":\"a\",\"B\":\"b\",\"C\":\"c\",\"D\":2,\"E\":\"e\"}

I want to convert above string into struct:

{
  A string
  B string
  C string
  D int
  E string
}

Im not sure how to do that as I have do the quote and unquote but seems no success yet.

答案1

得分: 7

将您的输入字符串在解码之前进行包装,像这样:

s, err := strconv.Unquote(`"` + yourstring + `"`)

然后您可以继续进行解组操作。

英文:

Wrap your incoming string before unquoting it like this:

s,err := strconv.Unquote(`"`+yourstring+`"`)

Then you can proceed with unmarshalling it.

答案2

得分: 2

有点取巧,但输入字符串的编码方式与它在JSON对象中的编码方式相同,所以你可以这样做:

x := json.RawMessage(`"{"A":"a","B":"b","C":"c","D":2,"E":"e"}"`)
var v string
err := json.Unmarshal(x, &v)
var x MyStruct
json.Unmarshal([]byte(v), &x)
英文:

Somewhat of a hack, but the input string is how it would be encoded if it was in a JSON object, so you can do this:

x:=json.RawMessage(`"{\"A\":\"a\",\"B\":\"b\",\"C\":\"c\",\"D\":2,\"E\":\"e\"}"`)
var v string
err:=json.Unmarshal(x,&v)
var x MyStruct
json.Unmarshal([]byte(v),&x)

答案3

得分: 1

你可以使用strconv包中的Unquote方法,就像mfathirirhas建议的那样。我创建了一个简单的代码来演示你的情况,代码如下:

package main

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

type response struct {
	A string
	B string
	C string
	D int
	E string
}

func main() {

	str := (`"{"A":"a","B":"b","C":"c","D":2,"E":"e"}"`)
	fmt.Printf(str)
	s, err := strconv.Unquote(str)
	fmt.Println()
	fmt.Println(s, err)
	var resp response
	if err := json.Unmarshal([]byte(s), &resp); err != nil {
		panic(err)
	}
	fmt.Println(resp)

}

输出结果:

"{"A":"a","B":"b","C":"c","D":2,"E":"e"}"
{"A":"a","B":"b","C":"c","D":2,"E":"e"} <nil>
{a b c 2 e}

这段代码使用Unquote方法将字符串中的引号去除,并将其解析为一个结构体对象。然后,使用json.Unmarshal方法将解析后的字符串转换为response类型的对象。最后,打印出解析后的结果。

英文:

You can make use of Unquote method of strconv as suggested by mfathirirhas, I have created a small code depicting you scenario as follows:

package main

import (
	&quot;encoding/json&quot;
	&quot;fmt&quot;
	&quot;strconv&quot;
)

type response struct {
	A string
	B string
	C string
	D int
	E string
}

func main() {

	str := (`&quot;{\&quot;A\&quot;:\&quot;a\&quot;,\&quot;B\&quot;:\&quot;b\&quot;,\&quot;C\&quot;:\&quot;c\&quot;,\&quot;D\&quot;:2,\&quot;E\&quot;:\&quot;e\&quot;}&quot;`)
	fmt.Printf(str)
	s, err := strconv.Unquote(str)
	fmt.Println()
	fmt.Println(s, err)
	var resp response
	if err := json.Unmarshal([]byte(s), &amp;resp); err != nil {
		panic(err)
	}
	fmt.Println(resp)

}

Output:

&quot;{\&quot;A\&quot;:\&quot;a\&quot;,\&quot;B\&quot;:\&quot;b\&quot;,\&quot;C\&quot;:\&quot;c\&quot;,\&quot;D\&quot;:2,\&quot;E\&quot;:\&quot;e\&quot;}&quot;
{&quot;A&quot;:&quot;a&quot;,&quot;B&quot;:&quot;b&quot;,&quot;C&quot;:&quot;c&quot;,&quot;D&quot;:2,&quot;E&quot;:&quot;e&quot;} &lt;nil&gt;
{a b c 2 e}

huangapple
  • 本文由 发表于 2021年7月8日 10:07:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/68294767.html
匿名

发表评论

匿名网友

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

确定