英文:
Appending JSON struct to JSON file Golang
问题
我有一个用户的JSON文档,其中包含ID号码、电话号码和电子邮件。在输入另一个ID、电话和电子邮件后,我想将新用户的信息添加到文件中。我有一个只包含{ID: #, Phone: #, Email: #}结构的变量,这个变量运行良好。但是我的JSON文件变成了这样:
[{"ID":"ABCD","Phone":1234567890,"Email":"johndoe@test.com"}]
[{"ID":"EFGH","Phone":1234567890,"Email":"johndoe@test.com"}]
[{"ID":"IJKL","Phone":1234567890,"Email":"johndoe@test.com"}]
[{"ID":"MNOP","Phone":1234567890,"Email":"johndoe@test.com"}]
[{"ID":"QRST","Phone":1234567890,"Email":"johndoe@test.com"}]
[{"ID":"UVWX","Phone":1234567890,"Email":"johndoe@test.com"}]
所以我能够将信息追加到文档中,但它是一个由方括号[]括起来的新的JSON结构。以下是我的代码。我省略了实际的哈希ID。
func ToIds(e string, p int64) {
hashed := GenId()
var jsonText = []byte(`[
{"ID": "", "Phone": 0, "Email": ""}
]`)
var I Identification
err := json.Unmarshal([]byte(jsonText), &I)
if err != nil {
fmt.Println(err)
}
I[0].ID = hashed
I[0].Phone = p
I[0].Email = e
result, error := json.Marshal(I)
if error != nil {
fmt.Println(error)
}
f, erro := os.OpenFile("FILE_PATH", os.O_APPEND|os.O_WRONLY, 0666)
if erro != nil {
fmt.Println(erro)
}
n, err := io.WriteString(f, string(result))
if err != nil {
fmt.Println(n, err)
}
}
这是我的Identification结构:
type Identification []struct {
ID string
Phone int64
Email string
}
基本上,我想要外部的方括号,而在这些方括号内部,我想要追加多个用户。类似于这样:
[
{"id":"A", "phone":17145555555, "email":"tom@gmail.com"},
{"id":"B","phone":15555555555,"email":"p@gmail.com"},
{"id":"C","phone":14155555555,"email":"bradley@gmail.com"},
{"id":"D","phone":17135555555,"email":"g@gmail.com"},
{"id":"E","phone":17125555555,"email":"ann@gmail.com"},
{"id":"F","phone":17125555555,"email":"sam@gmail.com"},
{"id":"G","phone":14055555555,"email":"john@gmail.com"},
{"id":"H","phone":13105555555,"email":"lisa@gmail.com"}
]
英文:
I have a JSON document of Users where they have an ID#, Phone#, and Email. Upon input of another ID, Phone, and Email, I want to take a new user's information and append it to the file. I have a structure that only contains an {ID: #, Phone: #, Email: #} and that's working fine. But my JSON file becomes as such:
[{"ID":"ABCD","Phone":1234567890,"Email":"johndoe@test.com"}]
[{"ID":"EFGH","Phone":1234567890,"Email":"johndoe@test.com"}]
[{"ID":"IJKL","Phone":1234567890,"Email":"johndoe@test.com"}]
[{"ID":"MNOP","Phone":1234567890,"Email":"johndoe@test.com"}]
[{"ID":"QRST","Phone":1234567890,"Email":"johndoe@test.com"}]
[{"ID":"UVWX","Phone":1234567890,"Email":"johndoe@test.com"}]
So I'm able to append to the document, but it's a new JSON structure enclosed by brackets []. Below is my code. I omitted the actual hashed ID.
func ToIds(e string, p int64) {
hashed := GenId()
var jsonText = []byte(`[
{"ID": "", "Phone": 0, "Email": ""}
]`)
var I Identification
err := json.Unmarshal([]byte(jsonText), &I)
if err != nil {
fmt.Println(err)
}
I[0].ID = hashed
I[0].Phone = p
I[0].Email = e
result, error := json.Marshal(I)
if error != nil {
fmt.Println(error)
}
f, erro := os.OpenFile("FILE_PATH", os.O_APPEND|os.O_WRONLY, 0666)
if erro != nil {
fmt.Println(erro)
}
n, err := io.WriteString(f, string(result))
if err != nil {
fmt.Println(n, err)
}
}
This may be of use, here is my Identification struct.
type Identification []struct {
ID string
Phone int64
Email string
}
Essentially, I want the outside brackets, and inside those brackets I want to append multiple users. Something like this:
[
{"id":"A", "phone":17145555555, "email":"tom@gmail.com"},
{"id":"B","phone":15555555555,"email":"p@gmail.com"},
{"id":"C","phone":14155555555,"email":"bradley@gmail.com"},
{"id":"D","phone":17135555555,"email":"g@gmail.com"},
{"id":"E","phone":17125555555,"email":"ann@gmail.com"},
{"id":"F","phone":17125555555,"email":"sam@gmail.com"},
{"id":"G","phone":14055555555,"email":"john@gmail.com"},
{"id":"H","phone":13105555555,"email":"lisa@gmail.com"}
]
答案1
得分: 10
要实现你的输出,可以按照以下方式定义结构体:
type Identification struct {
ID string
Phone int64
Email string
}
然后按照以下方式执行操作:
// 定义 Identification 的切片
var idents []Identification
// 反序列化
err := json.Unmarshal([]byte(jsonText), &idents)
// 添加更多的值
idents = append(idents, Identification{ID: "ID", Phone: 15555555555, Email: "Email"})
// 现在进行序列化
result, err := json.Marshal(idents)
// 现在 result 中就是你想要的 JSON 结构
以上是上述说明的示例程序:https://play.golang.org/p/67dqOaCWHI
英文:
To achieve your output, define struct as follows-
type Identification struct {
ID string
Phone int64
Email string
}
And perform operation as follows-
// define slice of Identification
var idents []Identification
// Unmarshall it
err := json.Unmarshal([]byte(jsonText), &idents)
// add further value into it
idents = append(idents, Identification{ID: "ID", Phone: 15555555555, Email: "Email"})
// now Marshal it
result, error := json.Marshal(idents)
// now result has your targeted JSON structure
Sample program of above explanation https://play.golang.org/p/67dqOaCWHI
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论