Best way to modify a json file?

blov · · 382 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;ve got a json file that I&#39;m basically using as a database for this little todolist program I&#39;m writing for myself. My question is, what would be the best way to write to the file, and modify its contents? Currently I&#39;ve got a suitable struct that holds all the contents of todos, and I&#39;d like to be able to insert more todos, create more categories to which various todos would belong, etc. </p> <p>Is this where the Marshal function would come in handy? I&#39;m just not exactly sure how I&#39;d take the existing contents, add to/modify them, then marshal back/write back to the json file. Any pointers would be greatly appreciated!</p> <hr/>**评论:**<br/><br/>sairamk: <pre><p>Q1) My question is, what would be the best way to write to the file, and modify its contents? </p> <p>I have done something similar in yaml. (changing the fields to json would help instead of yaml - reference here - <a href="https://github.com/sairam/gitnotify/blob/45e6bbf96143442032d483bf1d27ae9ada0df660/repos.go#L118-L160" rel="nofollow">https://github.com/sairam/gitnotify/blob/45e6bbf96143442032d483bf1d27ae9ada0df660/repos.go#L118-L160</a> ) </p> <p>I had a struct which has two methods named <code>load()</code> and <code>save()</code> methods which I called to load the contents of the file based on the logged in user&#39;s attributes and save at the end of the request. </p> <p>Code reference: <a href="https://github.com/sairam/gitnotify/blob/45e6bbf96143442032d483bf1d27ae9ada0df660/repos.go#L118-L160" rel="nofollow">https://github.com/sairam/gitnotify/blob/45e6bbf96143442032d483bf1d27ae9ada0df660/repos.go#L118-L160</a> </p> <p>Q2) How to upgrade or use when adding new fields?</p> <p>If you are modifying your struct, the empty fields are usually ignored or treated as nil. You may need to initialise. A better way is to have a <code>version</code> field in your json structure to understand which fields are present and which needed to be added. An alternative crude way to do is to backfill whenever the data is empty as part of the <code>load()</code>. <a href="https://github.com/sairam/gitnotify/blob/45e6bbf96143442032d483bf1d27ae9ada0df660/repos.go#L132-L148" rel="nofollow">code reference</a></p></pre>nathj07: <pre><p>There is an open source library that allows for just this is case. Though for the life of me I can&#39;t remember what it&#39;s called, sorry about that. It may be gabs, but I&#39;m not 100% sure of that.</p> <p>My other comment/question is if you are using JSON as a database why not just use a database? You&#39;ll have much more flexibility that way and a solution that is easy to reason about in terms of data management.</p></pre>ConfuciusBateman: <pre><p>I think I stumbled upon gabs, it definitely looked like it would do the job. I ended up figuring it all out by trial and error, however, so I didn&#39;t use it. I&#39;m definitely going to look into using an actual database in the future, but this was just a little fun project mainly intended to get me more familiar with Go, and working from a JSON file in whatever my directory is seemed simple enough to get going.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

382 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传