mysql解析json/数组

mysql在5.7开始支持json解析了 也可以解析数组哦!

直接上demo:

 SELECT Substr(col, 2, Length(col) - 2), Length(col)
         FROM   (SELECT Json_extract(Json_extract(Json_extract(state, "$.tpl"),"$.items"
                            ), "$[0].url")
               AS col
        FROM   page
        ORDER  BY id DESC
        LIMIT  100) t;

JSON_EXTRACT可以解析sql , tpl就是你json的key值

如果是数组,用$[*].url  或者 $[0].url  获取全部的value 或者某个下标的url

下面这个demo可以直接复制到sql运行:

 select JSON_EXTRACT(JSON_EXTRACT(JSON_EXTRACT('{"tpl":{"items":[{"type":"image","config":{"expandable":true,"linkAble":true},"url":"https://fs.esf.fangdd.net/test/FiZ0OtkhTZoD7fOtkp55SnuLGiKu.png?imageView2/2/w/750","id":1542348252537},{"type":"image","config":{"expandable":true,"linkAble":true},"url":"https://fs.esf.fangdd.net/test/FlR1VDQWEzD406NosLFrJUez4g_X.png?imageView2/2/w/750","id":1542348263477},{"type":"image","config":{"expandable":true,"linkAble":true},"url":"https://fs.esf.fangdd.net/test/FhMuYkWvnoMbv8I1dlQbm1KaX5Kn.png?imageView2/2/w/750","id":1542348269599},{"type":"image","config":{"expandable":true,"linkAble":true},"url":"https://fs.esf.fangdd.net/test/FlgR4IUNElPbcgjN2re_9A8jX30v.png?imageView2/2/w/750","id":1542348276124},{"type":"image","config":{"expandable":true,"linkAble":true},"url":"https://fs.esf.fangdd.net/test/FpXF8ETHxU8aqriiKbsYDjnu2Xd5.png?imageView2/2/w/750","id":1542348282561},{"type":"image","config":{"expandable":true,"linkAble":true},"url":"https://fs.esf.fangdd.net/test/FkUz5m7Jd6kE2slSyreDucozc3XH.png?imageView2/2/w/750","id":1542348288150,"link":"http://www.baidu.com"}],"bottomItems":[],"title":"demo2","description":"","wxLogo":"","bodyStyleInline":{},"bg":"","bgType":"","bottomStyleInline":{},"bottomBg":"","bottomBgType":"","uuid":"aaef8dfe-256a-4559-aec9-95d1fcdcf830","activeItemsName":"items","activeImgType":"","authInfo":{"role_list":[{"name":"test","access_key_list":[]},{"name":"审核人员","access_key_list":[]}],"city_list":[],"userId":3108779,"userName":"zhangyusheng","email":"zhangyusheng@xxx.com","mobile":"123123","trueName":"张昱升","isEmployee":true}}}', "$.tpl"), "$.items"), "$[0].url");

我们来分析一下

原始json为

{
    "tpl":{
        "items":[
            {
                "type":"image",
                "config":{
                    "expandable":true,
                    "linkAble":true
                },
                "url":"https://fs.esf.fangdd.net/test/FiZ0OtkhTZoD7fOtkp55SnuLGiKu.png?imageView2/2/w/750",
                "id":1542348252537
            },
            {
                "type":"image",
                "config":{
                    "expandable":true,
                    "linkAble":true
                },
                "url":"https://fs.esf.fangdd.net/test/FlR1VDQWEzD406NosLFrJUez4g_X.png?imageView2/2/w/750",
                "id":1542348263477
            },
            {
                "type":"image",
                "config":{
                    "expandable":true,
                    "linkAble":true
                },
                "url":"https://fs.esf.fangdd.net/test/FhMuYkWvnoMbv8I1dlQbm1KaX5Kn.png?imageView2/2/w/750",
                "id":1542348269599
            },
            {
                "type":"image",
                "config":{
                    "expandable":true,
                    "linkAble":true
                },
                "url":"https://fs.esf.fangdd.net/test/FlgR4IUNElPbcgjN2re_9A8jX30v.png?imageView2/2/w/750",
                "id":1542348276124
            },
            {
                "type":"image",
                "config":{
                    "expandable":true,
                    "linkAble":true
                },
                "url":"https://fs.esf.fangdd.net/test/FpXF8ETHxU8aqriiKbsYDjnu2Xd5.png?imageView2/2/w/750",
                "id":1542348282561
            },
            {
                "type":"image",
                "config":{
                    "expandable":true,
                    "linkAble":true
                },
                "url":"https://fs.esf.fangdd.net/test/FkUz5m7Jd6kE2slSyreDucozc3XH.png?imageView2/2/w/750",
                "id":1542348288150,
                "link":"http://www.baidu.com"
            }
        ],
        "bottomItems":[

        ],
        "title":"demo2",
        "description":"",
        "wxLogo":"",
        "bodyStyleInline":{

        },
        "bg":"",
        "bgType":"",
        "bottomStyleInline":{

        },
        "bottomBg":"",
        "bottomBgType":"",
        "uuid":"aaef8dfe-256a-4559-aec9-95d1fcdcf830",
        "activeItemsName":"items",
        "activeImgType":"",
        "authInfo":{
            "role_list":[
                {
                    "name":"test",
                    "access_key_list":[

                    ]
                },
                {
                    "name":"审核人员",
                    "access_key_list":[

                    ]
                }
            ],
            "city_list":[

            ],
            "userId":3108779,
            "userName":"zhangyusheng",
            "email":"zhangyusheng@xxx.com",
            "mobile":"23123",
            "trueName":"张昱升",
            "isEmployee":true
        }
    }
}

$.tpl就是获取tpl这个键key

$[0].url  就是获取[{url:1},{url:2}] 这个数组第一个对象的url值 也就是1

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值