From 5e3e62a38f64ee3ae2c9862ab5521126178e06b9 Mon Sep 17 00:00:00 2001 From: Jimmy99 Date: Wed, 6 Apr 2016 11:29:49 +0200 Subject: [PATCH 1/2] Update 07.2.md typographical errors and improved readability --- en/07.2.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/en/07.2.md b/en/07.2.md index 6066b18c..ca1ca463 100644 --- a/en/07.2.md +++ b/en/07.2.md @@ -57,7 +57,7 @@ You may have noticed that all fields that are going to be assigned should be exp When we know what kind of JSON to expect in advance, we can parse it to a specific struct. But what if we don't know? -We know that an interface{} can be anything in Go, so it is the best container to save our JSON of unknown format. The JSON package uses `map[string]interface{}` and `[]interface{}` to save all kinds of JSON objects and arrays. Here is a list of JSON mapping relations: +We know that an interface{} can be anything in Go, so it is the best container to save our JSON of unknown format. The JSON package uses `map[string]interface{}` and `[]interface{}` to save all kinds of JSON objects and arrays. Here is a list of JSON mapping relations:typographical errors and improved readability - `bool` represents `JSON booleans`, - `float64` represents `JSON numbers`, @@ -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 From 3f398353b8d743118640ae10d380126910375a0b Mon Sep 17 00:00:00 2001 From: Jimmy99 Date: Sun, 10 Apr 2016 19:04:50 +0200 Subject: [PATCH 2/2] Update 07.2.md Amended unintentional typo --- en/07.2.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/en/07.2.md b/en/07.2.md index ca1ca463..58707d3d 100644 --- a/en/07.2.md +++ b/en/07.2.md @@ -57,7 +57,7 @@ You may have noticed that all fields that are going to be assigned should be exp When we know what kind of JSON to expect in advance, we can parse it to a specific struct. But what if we don't know? -We know that an interface{} can be anything in Go, so it is the best container to save our JSON of unknown format. The JSON package uses `map[string]interface{}` and `[]interface{}` to save all kinds of JSON objects and arrays. Here is a list of JSON mapping relations:typographical errors and improved readability +We know that an interface{} can be anything in Go, so it is the best container to save our JSON of unknown format. The JSON package uses `map[string]interface{}` and `[]interface{}` to save all kinds of JSON objects and arrays. Here is a list of JSON mapping relations: - `bool` represents `JSON booleans`, - `float64` represents `JSON numbers`,