<p>Say I want to unmarshal a JSON list to a list of User structs, is there a way to only unmarshal objects with a field "Age" greater than "18"? Or I have to manually filter the User list?</p>
<hr/>**评论:**<br/><br/>Bake_Jailey: <pre><p><a href="https://golang.org/pkg/encoding/json/#RawMessage" rel="nofollow">json.RawMessage</a> might be of interest.</p></pre>slmyers: <pre><p>RemindMe! 7 days "ycb issue"</p></pre>RemindMeBot: <pre><p>I will be messaging you on <a href="http://www.wolframalpha.com/input/?i=2017-10-11%2018:40:43%20UTC%20To%20Local%20Time" rel="nofollow"><strong>2017-10-11 18:40:43 UTC</strong></a> to remind you of <a href="https://www.reddit.com/r/golang/comments/741xeb/is_possible_to_do_conditional_unmarshalling_for/dnwo4fa" rel="nofollow"><strong>this link.</strong></a></p>
<p><a href="http://np.reddit.com/message/compose/?to=RemindMeBot&subject=Reminder&message=%5Bhttps://www.reddit.com/r/golang/comments/741xeb/is_possible_to_do_conditional_unmarshalling_for/dnwo4fa%5D%0A%0ARemindMe!%20%207%20days" rel="nofollow"><strong>CLICK THIS LINK</strong></a> to send a PM to also be reminded and to reduce spam.</p>
<p><sup>Parent commenter can </sup> <a href="http://np.reddit.com/message/compose/?to=RemindMeBot&subject=Delete%20Comment&message=Delete!%20dnwo5bu" rel="nofollow"><sup>delete this message to hide from others.</sup></a></p>
<hr/>
<table><thead>
<tr>
<th><a href="http://np.reddit.com/r/RemindMeBot/comments/24duzp/remindmebot_info/" rel="nofollow"><sup>FAQs</sup></a></th>
<th><a href="http://np.reddit.com/message/compose/?to=RemindMeBot&subject=Reminder&message=%5BLINK%20INSIDE%20SQUARE%20BRACKETS%20else%20default%20to%20FAQs%5D%0A%0ANOTE:%20Don't%20forget%20to%20add%20the%20time%20options%20after%20the%20command.%0A%0ARemindMe!" rel="nofollow"><sup>Custom</sup></a></th>
<th><a href="http://np.reddit.com/message/compose/?to=RemindMeBot&subject=List%20Of%20Reminders&message=MyReminders!" rel="nofollow"><sup>Your Reminders</sup></a></th>
<th><a href="http://np.reddit.com/message/compose/?to=RemindMeBotWrangler&subject=Feedback" rel="nofollow"><sup>Feedback</sup></a></th>
<th><a href="https://github.com/SIlver--/remindmebot-reddit" rel="nofollow"><sup>Code</sup></a></th>
<th><a href="https://np.reddit.com/r/RemindMeBot/comments/4kldad/remindmebot_extensions/" rel="nofollow"><sup>Browser Extensions</sup></a></th>
</tr>
</thead><tbody>
</tbody></table></pre>titpetric: <pre><p>To be very precise, you can't do conditional unmarshalling with your limitation. What you can do is parse the user list into <code>[]json.RawMessage</code>, and then loop through that, decode the actual <code>User</code> struct, and if Age is larger than 18 append it to a slice of users. The difference of this approach vs. using <code>[]User</code> is, that each user struct will be unmarshalled, but you would not allocate each onto a slice, but only the ones that fit your conditions.</p>
<p>Why? The main reason is that <code>Age</code> is a field directly inside the <code>User</code> struct. That means that you have to decode the complete User struct to read the field value. Optionally, you could decode it into:</p>
<pre><code>type UserOnlyAge struct {
Age int `json:"age"`
}
</code></pre>
<p>And thus check Age before deciding to unmarshal the complete <code>User</code> struct. In term of performance, I would expect that the penalty would be similar to parsing the complete struct twice. You could resort to <a href="https://github.com/mailru/easyjson" rel="nofollow">a third party json unmarshaller</a> to test what works better for your case.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传