Problem solving: Golang's json package failed to parse fields

introduction

This problem occurred when parsing a json configuration file. After troubleshooting for a long time, I finally solved this strange problem with Gou Xun.

text

In fact, this problem is not difficult, because Golang comes with the json package, so what we have to do is to add corresponding tags to the structure that needs to be parsed, but there may be some hidden bugs without any Error.

I have encountered two problems in total. The first one is easier to solve, and most of the online solutions describe the first problem; but the second problem is very strange, because there are problems in the project, I wrote A test code did not reproduce successfully.

The first solution: the corresponding fields of the structure that need to be parsed should be capitalized .

Let's first look at a piece of code:

---test.json
{
    
    
  "page": 1,
  "fruits": ["apple", "peach"]
}

---test_json.go
type response struct {
    
    
	number int
	Page   int      `json:page`
	fruits []string `json:fruits`
}

func main(){
    
    
	data, _ := io.ReadFile("test.json")
	res := response{
    
    }
	json.Unmarshal(data, &res)
	fmt.Println(res)
}

ouput:
{
    
    0 1 []}

We can see that there is a problem when the first letter is lowercase, and the solution is very easy, that is, you need to change the beginning of the field to uppercase. Of course, this has drawbacks, because the fields that generally need to be read from the configuration file are generally related to the configuration Related, and these should generally be set to private, C++ can be solved very well, this may be the reason why go is not as free as C++, of course, this may also be the reason why C++ does not have a json library like go.

The second solution: Goland's json parsing must either add double quotation marks or not add double quotation marks. The name cannot contain underscores, ie_ .

That is, the above structure should be written like this:

type response struct {
    
    
	number int
	Page   int      `json:"page"`
	fruits []string `json:"fruits"`
}

That's it. Although the test code can correctly parse the slice without double quotation marks, there is a problem with the structure in the project. I extracted the json field from the structure in the project to reproduce it. Wrong:

---test.json
{
    
    
  "servers_address": ["localhost:8901","localhost:8902"],
  "myport" : ":8900",
  "maxreries" : 13,
  "timeout_entry" : 200
}

---test_json.go
type response struct {
    
    
	Maxreries      int      `json:maxreries`
	ServersAddress []string `json:servers_address`
	MyPort         string   `json:myport`
	TimeOutEntry   int		`json:timeout_entry`
}

func main(){
    
    
	data, _ := io.ReadFile("test.json")
	res := response{
    
    }
	json.Unmarshal(data, &res)
	fmt.Println(res)
}

ouput:
{
    
    13 [] :8900 0}

We can see that the parsing failed ServersAddressand the TimeOutEntryparsing failed. This is not the failure of parsing slices. There is also a problem with int. Let us add double quotes to see:

ouput:
{
    
    13 [localhost:8901 localhost:8902] :8900 200}

We can see that the analysis was successful.

So why does it sometimes succeed in parsing without double quotation marks, but sometimes not? Because it is not the failure to parse the slice, an error also occurs when parsing int.

Observing the two problematic fields, we found that they have one thing in common, that is, they are both included in the json tag _. Let us remove the execution and have a look:

{
    
    13 [localhost:8901 localhost:8902] :8900 200}

Check out the error! That is, Golang's json parsing cannot parse _the fields in the name when the structure json tag is not enclosed in double quotes !

This problem is estimated to be an extremely implicit problem, and it is not described in the official documents. There is no article describing this problem on the Internet before this. I hope that reading this article can help you solve this confusing problem.

reference:

Guess you like

Origin blog.csdn.net/weixin_43705457/article/details/109191762