内容不固定的json如何解析呀?新手,搜索了集中方法,还是不知道如何搞

123445 · 2019-11-29 17:53:15 · 1977 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2019-11-29 17:53:15 的主题,其中的信息可能已经有所发展或是发生改变。

json1

{"time":1575018625572,"code":10006,"msg":"未查询到对应信息"}

json2

{
    "time": 1575018616175,
    "code": 0,
    "msg": "成功",
    "data": {
        "list": [
            {
                "id": 23638664,
                "goodsId": "578575659556",
                "title": "Milk Skin/奶肌牛奶素颜霜 多效保湿护肤遮瑕提亮面霜清新裸妆女",
                "dtitle": "Milk Skin/奶肌牛奶素颜霜",
                "originalPrice": 138,
                "actualPrice": 108,
                "shopType": 1,
                "goldSellers": 0,
                "monthSales": 3660,
                "twoHoursSales": 2,
                "dailySales": 12,
                "commissionType": 3,
                "desc": "超火素颜霜,美白遮瑕,补水保湿,提亮肤色,一瓶搞定!天然提取物,进口成分,从根部滋润美白!仙女们的懒人大福利!快速打造美丽素颜,无需卸妆烦恼",
                "couponReceiveNum": 80,
                "couponLink": "https://uland.taobao.com/quan/detail?sellerId=4164798236&activityId=d360a99fe2664c128db165c9c87c20c2",
                "couponEndTime": "2019-12-04 23:59:59",
                "couponStartTime": "2019-11-28 00:00:00",
                "couponPrice": 30,
                "couponConditions": "31",
                "activityType": 1,
                "createTime": "2019-11-28 16:38:15",
                "mainPic": "https://img.alicdn.com/imgextra/i1/4164798236/O1CN01X8zWcx2Ai8UChON4v_!!4164798236.jpg",
                "marketingMainPic": "https://sr.ffquan.cn/relate_pic/o_1dq8vmfdn48j1c08lpj1io51gs7u.jpg",
                "sellerId": "4164798236",
                "cid": 2,
                "discounts": 0.78,
                "commissionRate": 20.02,
                "couponTotalNum": 1000,
                "haitao": 0,
                "activityStartTime": "",
                "activityEndTime": "",
                "shopName": "milkskin奶肌旗舰店",
                "shopLevel": 9,
                "descScore": 4.8,
                "brand": 0,
                "brandId": 1878088153,
                "brandName": "",
                "hotPush": 0,
                "teamName": "风神联盟",
                "itemLink": "https://detail.tmall.com/item.htm?id=578575659556",
                "tchaoshi": 0,
                "detailPics": "",
                "dsrScore": 4.8,
                "dsrPercent": 13.61,
                "shipScore": 4.8,
                "shipPercent": 40.83,
                "serviceScore": 4.8,
                "servicePercent": 40.63,
                "subcid": [
                    111746
                ],
                "quanMLink": 0,
                "hzQuanOver": 0,
                "yunfeixian": 1,
                "estimateAmount": -1,
                "tbcid": 50011980
            }
        ],
        "totalNum": 1,
        "pageId": "1"
    }
}

会返回上面两种类型的json,我用map[string]interface{}解析出来了,但是要访问里面的各个节点就难搞了,不方便使用,里面的数据的类型怎么搞呀?总是类型不对,解析个json类型都要把我搞死了。


有疑问加站长微信联系(非本文作者)

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

1977 次点击  
加入收藏 微博
11 回复  |  直到 2019-12-24 19:33:22
Krados
Krados · #1 · 5年之前

建議把回傳值 bind 到 struct 上, 請 google google 搜尋 golang decode json

jarlyyn
jarlyyn · #2 · 5年之前

data 使用json.rawmessage

可以再次解析

123445
123445 · #3 · 5年之前
KradosKrados #1 回复

建議把回傳值 bind 到 struct 上, 請 google google 搜尋 golang decode json

用strcut太麻烦了,字段又多。

Goooooooer
Goooooooer · #4 · 5年之前

map [string]interface{}

gu_817
gu_817 · #5 · 5年之前

你用 struct所有的字段 都包含,当你进行解析的时候是如果没有会自动过滤的 ·json:字段名· 你这样的可以多建立几个结构体 ,这里的首字母建议你都用大写的,方便跨包名使用,我懒就用小写了 type A struct{ Time int64 json:time code int msg string data //这个是结构体 }

type data struct{ list []list 因为list 是个数组格式 }

type list struct{ id int }

gu_817
gu_817 · #6 · 5年之前

QQ截图20191208114202.png 怕你看不懂,给你来个截图吧

czyt
czyt · #7 · 5年之前

rawmessage正解

wuxian
wuxian · #8 · 5年之前

上面的两个json是兼容的,字段太多,一个个敲,那是你姿势不对。推荐一个网站,自动json转struct

a7505553
a7505553 · #9 · 5年之前

map[string]interface{}

Justin19960208
Justin19960208 · #10 · 5年之前

"github.com/mitchellh/mapstructure"

saberlong
saberlong · #11 · 5年之前

看代码,感觉是准备写公共代码或者框架代码。外部主体结构确定,data不确定,使用json.RawMessage类型。 如果完全不确定的结构,可以使用gjson或jsoniter这类提供解析特定字段的第三方包

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