英文:
Wrapping json member fields to object
问题
我的目标是根据用户的请求向JSON中添加字段。
一切都很好,但是当使用以下代码显示字段时:
fmt.Printf("%s: %s\n", content.Date, content.Description)
会出现错误:
invalid character '{' after top-level value
这是因为在添加新字段后,文件的内容如下所示:
{"Date":"2017-03-20 10:46:48","Description":"new"}
{"Date":"2017-03-20 10:46:51","Description":"new .go"}
最大的问题在于写入文件的部分:
reminder := &Name{dateString[:19], text} //text - 输入字符串
newReminder, _ := json.Marshal(&reminder)
我不太清楚如何正确地做这个。
我的问题是如何将所有成员字段包装到一个对象中?
以及遍历成员字段的最佳方法是什么?
代码可以在这里找到:https://play.golang.org/p/NunV_B6sud
英文:
My objective is to add fields to json on user request.
Everything is great, but when displaying the fields with
fmt.Printf("%s: %s\n", content.Date, content.Description)
an error occurs:
invalid character '{' after top-level value
And that is because after adding new fields the file looks like this:
{"Date":"2017-03-20 10:46:48","Description":"new"}
{"Date":"2017-03-20 10:46:51","Description":"new .go"}
The biggest problem is with the writting to file
reminder := &Name{dateString[:19], text} //text - input string
newReminder, _ := json.Marshal(&reminder)
I dont really know how to do this properly
My question is how should I wrap all member fields into one object?
And what is the best way to iterate through member fields?
The code is available here: https://play.golang.org/p/NunV_B6sud
答案1
得分: 0
你应该将提醒存储在json文件中的数组中,就像@Gerben Jacobs提到的那样。每次你想要向数组中添加一个新的提醒时,你需要读取rem.json的全部内容,在Go中添加新的提醒,截断文件,并将新的切片写入文件中。这里有一个快速实现的示例:https://play.golang.org/p/UKR91maQF2。
如果你有很多提醒,并且读取、解码、编码和写入整个内容的过程变得很繁琐,你可以打开文件,实现一种方法来截断文件内容中的最后一个],然后只写入, + 新的提醒 + ]。
英文:
You should store the reminders into an array inside the json file, as mentioned by @Gerben Jacobs, and then, every time you want to add a new reminder to the array you need to read the full contents of rem.json, append the new reminder in Go, truncate the file, and write the new slice into the file. Here's a quick implentation https://play.golang.org/p/UKR91maQF2.
If you have lots of reminders and the process of reading, decoding, encoding, and writing the whole content becomes a pain you could open the file, implement a way to truncate only the last ] from the file contents, and then write only , + new reminder + ].
答案2
得分: 0
所以经过一些研究,go-nuts小组的人帮助我,并建议我使用一个流式JSON解析器来逐个解析项目。
所以我需要改变我的提醒列表函数:
func listReminders() error {
f, err := os.Open("rem.json")
if err != nil {
return err
}
dec := json.NewDecoder(f)
for {
var content Name
switch dec.Decode(&content) {
case nil:
fmt.Printf("%#v\n", content)
case io.EOF:
return nil
default:
return err
}
}
}
现在一切都按照我想要的方式工作了。
英文:
So after some research, people in the go-nuts group helped me and suggested me to use a streaming json parser that parses items individually.
So I needed to change my reminder listing function:
func listReminders() error {
f, err := os.Open("rem.json")
if err != nil {
return err
}
dec := json.NewDecoder(f)
for {
var content Name
switch dec.Decode(&content) {
case nil:
fmt.Printf("%#v\n", content)
case io.EOF:
return nil
default:
return err
}
}
}
Now everything works the way I wanted.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论