~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to doc/articles/json_and_go.html

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
{{code "/doc/progs/json1.go" `/m :=/`}}
44
44
 
45
45
<p>
46
 
we can marshal a JSON-encoded version of m using <code>json.Marshal</code>:
 
46
we can marshal a JSON-encoded version of <code>m</code> using <code>json.Marshal</code>:
47
47
</p>
48
48
 
49
49
{{code "/doc/progs/json1.go" `/b, err :=/`}}
82
82
 
83
83
<p>
84
84
The json package only accesses the exported fields of struct types (those that
85
 
begin with an uppercase letter). Therefore only the the exported fields of a
86
 
struct will be present in the JSON output.
 
85
begin with an uppercase letter). Therefore only the exported fields of a struct
 
86
will be present in the JSON output.
87
87
</p>
88
88
 
89
89
<p>
130
130
 
131
131
<ul>
132
132
<li>
133
 
An exported field with a tag of <code>"Foo"</code> (see the
 
133
An exported field with a tag of <code>`json:"Foo"`</code> (see the
134
134
<a href="/ref/spec#Struct_types">Go spec</a> for more on struct tags),
135
135
</li>
136
136
<li>
151
151
 
152
152
<p>
153
153
<code>Unmarshal</code> will decode only the fields that it can find in the
154
 
destination type.  In this case, only the Name field of m will be populated,
155
 
and the Food field will be ignored. This behavior is particularly useful when
156
 
you wish to pick only a few specific fields out of a large JSON blob. It also
157
 
means that any unexported fields in the destination struct will be unaffected
158
 
by <code>Unmarshal</code>.
 
154
destination type.  In this case, only the <code>Name</code> field of m will be
 
155
populated, and the <code>Food</code> field will be ignored. This behavior is
 
156
particularly useful when you wish to pick only a few specific fields out of a
 
157
large JSON blob. It also means that any unexported fields in the destination
 
158
struct will be unaffected by <code>Unmarshal</code>.
159
159
</p>
160
160
 
161
161
<p>
163
163
</p>
164
164
 
165
165
<p>
166
 
<b>Generic JSON with interface{}</b>
 
166
<b>Generic JSON with <code>interface{}</code></b>
167
167
</p>
168
168
 
169
169
<p>
190
190
 
191
191
{{code "/doc/progs/json2.go" `/switch v/` `/STOP/`}}
192
192
 
193
 
 
 
193
<p>
194
194
The json package uses <code>map[string]interface{}</code> and
195
195
<code>[]interface{}</code> values to store arbitrary JSON objects and arrays;
196
196
it will happily unmarshal any valid JSON blob into a plain
197
197
<code>interface{}</code> value.  The default concrete Go types are:
 
198
</p>
198
199
 
199
200
<ul>
200
201
<li>