Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
5ede9df5
Commit
5ede9df5
authored
Dec 19, 2011
by
Andrew Gerrand
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
encoding/json: examples for Marshal and Unmarshal
R=golang-dev, r CC=golang-dev
https://golang.org/cl/5493075
parent
12f473f8
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
0 deletions
+48
-0
example_test.go
src/pkg/encoding/json/example_test.go
+48
-0
No files found.
src/pkg/encoding/json/example_test.go
0 → 100644
View file @
5ede9df5
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
json_test
import
(
"encoding/json"
"fmt"
"os"
)
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
func
ExampleMarshal
()
{
type
ColorGroup
struct
{
ID
int
Name
string
Colors
[]
string
}
group
:=
ColorGroup
{
ID
:
1
,
Name
:
"Reds"
,
Colors
:
[]
string
{
"Crimson"
,
"Red"
,
"Ruby"
,
"Maroon"
},
}
b
,
err
:=
json
.
Marshal
(
group
)
if
err
!=
nil
{
fmt
.
Println
(
"error:"
,
err
)
}
os
.
Stdout
.
Write
(
b
)
}
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
func
ExampleUnmarshal
()
{
var
jsonBlob
=
[]
byte
(
`[
{"Name": "Platypus", "Order": "Monotremata"},
{"Name": "Quoll", "Order": "Dasyuromorphia"}
]`
)
type
Animal
struct
{
Name
string
Order
string
}
var
animals
[]
Animal
err
:=
json
.
Unmarshal
(
jsonBlob
,
&
animals
)
if
err
!=
nil
{
fmt
.
Println
(
"error:"
,
err
)
}
fmt
.
Printf
(
"%+v"
,
animals
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment