したがって、jsonが正しくフォーマットされている以外、structおよびjson structタグを使用してjsonを作成する方法の例を次に示します。
適切なJSON
[ {キー:値、キー値}、 {キー:値、キー値} ]
何を持っていることは です{キー:値、キー値} {key:value、key value}
配列内の1つのオブジェクトではなく、2つの別々のオブジェクトです。
これをファイルから読み込み、データがサンプルのように返される場合は、改行を分割して各オブジェクトを分離し、それらを個別に非マーシャルする必要があります。
それ以外の場合は、以下の例をサーバーとして設定します。
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
)
type j struct {
Cl []string `json:"cl"`
Gr []string `json:"gr"`
Cr []string `json:"cr"`
}
func main() {
// create an instance of j as a slice
var data []j
// using a for loop for create dummy data fast
for i := 0; i < 5; i++ {
v := strconv.Itoa(i)
data = append(data, j{
Cl: []string{"foo " + v},
Gr: []string{"bar " + v},
Cr: []string{"foobar " + v},
})
}
// printing out json neatly to demonstrate
b, _ := json.MarshalIndent(data, "", " ")
fmt.Println(string(b))
// writing json to file
_ = ioutil.WriteFile("file.json", b, 0644)
// to append to a file
// create the file if it doesn't exists with O_CREATE, Set the file up for read write, add the append flag and set the permission
f, err := os.OpenFile("/var/log/debug-web.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0660)
if err != nil {
log.Fatal(err)
}
// write to file, f.Write()
f.Write(b)
// if you are doing alot of I/O work you may not want to write out to file often instead load up a bytes.Buffer and write to file when you are done... assuming you don't run out of memory when loading to bytes.Buffer
}
これは有効なJSONではありません。あなたがアイテムのグループを持っているなら、それらはコンマで区切られた配列で配列を必要とするでしょう。 – Joe
私はそれがどのように見えるべきかの例を教えてください。私はウェブ全体を見てきましたが、私はかなり混乱しています。 @Joe – HubbA
[{"cl": "[v1]"、...}、{"cl": "[v2]"、...}] – Joe