Merge pull request #629 from Jimmy99/patch-20

Update 07.2.md
This commit is contained in:
astaxie
2016-04-13 09:22:01 +08:00

View File

@@ -108,9 +108,9 @@ After asserted, you can use the following code to access data:
}
}
As you can see, we can parse JSON of an unknown format through interface{} and type assert now.
As you can see, we can now parse JSON of an unknown format through interface{} and type assertion.
The above example is the official solution, but type asserting is not always convenient. So, I recommend an open source project called `simplejson`, created and maintained by by bitly. Here is an example of how to use this project to deal with JSON of an unknown format:
The above example is the official solution, but type asserting is not always convenient. So, I recommend an open source project called `simplejson`, created and maintained by bitly. Here is an example of how to use this project to deal with JSON of an unknown format:
js, err := NewJson([]byte(`{
"test": {
@@ -185,7 +185,7 @@ Here are some points you need to keep in mind when trying to produce JSON:
- Field tags containing `"-"` will not be outputted.
- If a tag contains a customized name, Go uses this instead of the field name, like `serverName` in the above example.
- If a tag contains `omitempty`, this field will not be outputted if it is its zero-value.
- If a tag contains `omitempty`, this field will not be outputted if it is zero-value.
- If the field type is `bool`, string, int, `int64`, etc, and its tag contains `",string"`, Go converts this field to its corresponding JSON type.
Example:
@@ -198,7 +198,7 @@ Example:
ServerName string `json:"serverName"`
ServerName2 string `json:"serverName2,string"`
// If ServerIP is empty, it will not be outputed.
// If ServerIP is empty, it will not be outputted.
ServerIP string `json:"serverIP,omitempty"`
}
@@ -218,11 +218,11 @@ Output:
The `Marshal` function only returns data when it has succeeded, so here are some points we need to keep in mind:
- JSON only supports strings as keys, so if you want to encode a map, its type has to be `map[string]T`, where `T` is the type in Go.
- Types like channel, complex types and functions are not able to be encoded to JSON.
- Types like channel, complex types and functions are not capable of being encoded to JSON.
- Do not try to encode cyclic data, it leads to an infinite recursion.
- If the field is a pointer, Go outputs the data that it points to, or else outputs null if it points to nil.
In this section, we introduced how to decode and encode JSON data in Go. We also looked at one third-party project called `simplejson` which is for parsing JSON or unknown format. These are all useful concepts for developping web applications in Go.
In this section, we introduced how to decode and encode JSON data in Go. We also looked at one third-party project called `simplejson` which is useful for parsing JSON or unknown format. These are all useful concepts for developing web applications in Go.
## Links