json parsing help

xuanbao · · 473 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi <a href="/r/golang" rel="nofollow">r/golang</a>,</p> <p>So this probably gets asked a ton and I am still getting my sea legs here in the land of go. As a prolific googler I try to find everything first and read several posts first and have learned quite a bit already. I am not sure if Marshaling or Unmarshaling<br/> or simplejson/djson/(insertjsonlibhere) are the way to go here.</p> <p>I have a program that can read from a json file and return the information formatted. but I am also trying to parse and return array data based on input. Honestly I am lost here and I don&#39;t have a good way to form the question as everything I find is marshal/unmarshal but not parse and return clear text strings. Honestly this will help with 2 projects that I have so that is a thing as well.</p> <p>Here is my current work<br/> <a href="https://gist.github.com/parkervcp/2d9e42f571170b523b6cc70d9528d318" rel="nofollow">https://gist.github.com/parkervcp/2d9e42f571170b523b6cc70d9528d318</a></p> <hr/>**评论:**<br/><br/>GoHomeGrandmaUrHigh: <pre><p>Your code gets pretty close. After unmarshalling the JSON, you can just iterate through it as a normal Go struct. </p> <pre><code>func main() { // Read the line from standard input reader := bufio.NewReader(os.Stdin) fmt.Print(&#34;Enter text: &#34;) text, _ := reader.ReadString(&#39;\n&#39;) fmt.Println(text) commands := getCommands() for _, p := range commands { if p.Cmd == text { for _, line := range p.Lns { fmt.Println(line) } } } } </code></pre></pre>parkervcp: <pre><p>I am trying this and I only get my input as a response.</p> <pre><code>Enter text: Hello! Hello! </code></pre></pre>GoHomeGrandmaUrHigh: <pre><p>I adapted your code snippet for the Go Playground: <a href="https://play.golang.org/p/FDUD8IZ1io" rel="nofollow">https://play.golang.org/p/FDUD8IZ1io</a></p> <p>Changes:</p> <ul> <li>In your <code>getCommands()</code> I hard-coded the &#34;file contents&#34; (copy/pasted your JSON inputs)</li> <li>In main() I hard-coded the value for &#34;text&#34; because no standard input on the playground.</li> </ul> <p>With <code>text := &#34;test2&#34;</code>, I got as output:</p> <pre><code>This is one line this is another Program exited. </code></pre> <p>The other values &#34;test1&#34; and &#34;hello&#34; got me the expected outputs for those, too. Invalid values just didn&#39;t print anything, which makes sense because none of the <code>p.Cmd</code> was equal to it.</p></pre>morethanaprogrammer: <pre><p>If you are looking for a constant time lookup of the replies to a particular command, you could use a map, like so: <a href="https://play.golang.org/p/RXWZCFqYHf" rel="nofollow">https://play.golang.org/p/RXWZCFqYHf</a></p></pre>parkervcp: <pre><p>Reason I have the separate commands file it to have it easily modifiable by others than me.</p> <p>edit: this actually might be exactly what I was looking for though.</p></pre>morethanaprogrammer: <pre><p>You can still have the separate commands file. I was showing how you could do a lookup by command name.</p></pre>parkervcp: <pre><p>Working on that now.</p></pre>

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

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