Created
October 17, 2018 09:37
-
-
Save suganoo/d916806370ae5960a8555c0430284ff9 to your computer and use it in GitHub Desktop.
Golangでjsonを扱う
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"id" : 1, | |
"author" : "hogefuga", | |
"content" : "Hellooooooooo!!", | |
"comment" : { | |
"comment_id" : 11, | |
"message" : "very gooood" | |
}, | |
"books" : [ | |
{ | |
"book_id" : 23, | |
"title" : "hogefuga" | |
}, | |
{ | |
"book_id" : 54, | |
"title" : "wahahahahaha" | |
}, | |
{ | |
"book_id" : 76, | |
"title" : "ohhhnoooo" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"os" | |
) | |
type Post struct { | |
Id int `json:"id"` | |
Author string `json:"author"` | |
Content string `json:"content"` | |
Comment Comment `json:"comment"` | |
Books []Book `json:"books"` | |
} | |
type Comment struct { | |
Comment_id int `json:"comment_id"` | |
Message string `json:"message"` | |
} | |
type Book struct { | |
Book_id int `json:"book_id"` | |
Title string `json:"title"` | |
} | |
func main() { | |
// read json | |
fmt.Println("===== Read json =====") | |
json_file, err := os.Open("./json_sample.json") | |
if err != nil { | |
fmt.Println("Error opening", err) | |
return | |
} | |
defer json_file.Close() | |
json_data, err := ioutil.ReadAll(json_file) | |
if err != nil { | |
fmt.Println("Error reading", err) | |
return | |
} | |
var post_read Post | |
json.Unmarshal(json_data, &post_read) | |
fmt.Println(post_read) | |
fmt.Println(post_read.Comment.Message) | |
fmt.Println(post_read.Books[2].Title) | |
// write json | |
fmt.Println("===== Write json =====") | |
post_write := Post { | |
Id : 11111, | |
Author : "toriyama akira", | |
Comment : Comment { | |
Comment_id : 999999, | |
Message : "sugeeeeeee", | |
}, | |
Books : []Book { | |
Book { | |
Book_id : 1004, | |
Title : "naushika", | |
}, | |
Book { | |
Book_id : 2006, | |
Title : "akira", | |
}, | |
Book { | |
Book_id : 4005, | |
Title : "inachu", | |
}, | |
}, | |
} | |
fmt.Println(post_write) | |
output, err := json.MarshalIndent(&post_write, "", "\t") | |
if err != nil { | |
fmt.Println("Error marshalling", err) | |
return | |
} | |
err = ioutil.WriteFile("json_output.json", output, 0644) | |
if err != nil { | |
fmt.Println("Error write file", err) | |
return | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
===== Read json ===== | |
{1 hogefuga Hellooooooooo!! {11 very gooood} [{23 hogefuga} {54 wahahahahaha} {76 ohhhnoooo}]} | |
very gooood | |
ohhhnoooo | |
===== Write json ===== | |
{11111 toriyama akira {999999 sugeeeeeee} [{1004 naushika} {2006 akira} {4005 inachu}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"id": 11111, | |
"author": "toriyama akira", | |
"content": "", | |
"comment": { | |
"comment_id": 999999, | |
"message": "sugeeeeeee" | |
}, | |
"books": [ | |
{ | |
"book_id": 1004, | |
"title": "naushika" | |
}, | |
{ | |
"book_id": 2006, | |
"title": "akira" | |
}, | |
{ | |
"book_id": 4005, | |
"title": "inachu" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment