V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
garyox64
V2EX  ›  问与答

Go json 目前推荐用哪个库

  •  
  •   garyox64 · 2021-02-20 14:47:56 +08:00 · 1498 次点击
    这是一个创建于 1158 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Go 新手 求问现在 主流 json 用哪个 库 看了一些评测好像 小 json 用 jsonparser [ https://github.com/buger/jsonparser ] 大的用 jsoniter [ https://github.com/json-iterator/go ] ?

    然后我现在在用 jsoniter,没文档太痛苦了 建 struct 来序列化和反序列化 太麻烦了,因为我是复杂 json,想追求 py 的 json 使用体验, 目前使用过程中取值 可以用 Get + 路径取值,但是也只是取值而已,后面还要改值,再推送好像就没法子只能建 struct ? 看了部分文章,jsoniter 应该是可以 做到类似 py 的体验的??但是实在没找到详细的文档

    求助大佬指路或者 给个样例,需求:json 直接转 struct 之类的,像 py 一样的体验

    5 条回复    2021-02-20 18:01:50 +08:00
    shoaly
        1
    shoaly  
       2021-02-20 15:09:27 +08:00
    用过 php 或者 python 的 json 都非常开心

    到 go 这边 真的头痛... 简单的 json 结构还好, 就怕碰到那种层级特别复杂的 json, 各种数组嵌套, 数组套数组的时候 , 简直想哭... 昨天就碰到一个 json 例子 斗了半天才搞出来, 愿意练手的朋友 可以试试怎么在 go 里面 实例化一个 struct 对应到下面这个 json, 烦点就在 那个 inline_keyboard 里面的数据结构
    {"chat_id":"123","reply_markup":{"inline_keyboard":[[{"text":"text","callback_data":"callback:success"},{"text":"text","callback_data":"callback:fail"}]]},"reply_to_message_id":"112","text":"hello"}
    heiheidewo
        2
    heiheidewo  
       2021-02-20 15:10:51 +08:00
    ladit
        3
    ladit  
       2021-02-20 15:47:23 +08:00   ❤️ 1
    公司用的是 jsoniter,不过没有特别复杂的操作。

    @shoaly #1 如果结构是预先知道的,这个工具挺好用的: https://mholt.github.io/json-to-go/ ,如果是动态的,确实比较困扰。
    shoaly
        4
    shoaly  
       2021-02-20 17:20:39 +08:00
    @ladit 老哥 你真是可爱的人..... 这个地址我之前要知道 就不用花半个小时去凑 struct 了..
    LIncoln666
        5
    LIncoln666  
       2021-02-20 18:01:50 +08:00
    @heiheidewo 牛皮,收了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3443 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:39 · PVG 19:39 · LAX 04:39 · JFK 07:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.