mysql解析json 陣列

2021-09-01 09:26:31 字數 2150 閱讀 4835

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(',"url":"","id":1542348252537},,"url":"","id":1542348263477},,"url":"","id":1542348269599},,"url":"","id":1542348276124},,"url":"","id":1542348282561},,"url":"","id":1542348288150,"link":""}],"bottomitems":,"title":"demo2","description":"","wxlogo":"","bodystyleinline":{},"bg":"","bgtype":"","bottomstyleinline":{},"bottombg":"","bottombgtype":"","uuid":"aaef8dfe-256a-4559-aec9-95d1fcdcf830","activeitemsname":"items","activeimgtype":"","authinfo":,],"city_list":,"userid":3108779,"username":"zhangyusheng","email":"zhangyusheng@***.com","mobile":"123123","truename":"張昱公升","isemployee":true}}}', "$.tpl"), "$.items"), "$[0].url");
我們來分析一下

原始json為

,

"url":"",

"id":1542348252537},,

"url":"",

"id":1542348263477},,

"url":"",

"id":1542348269599},,

"url":"",

"id":1542348276124},,

"url":"",

"id":1542348282561},,

"url":"",

"id":1542348288150,

"link":""}],

"bottomitems":[

],"title":"demo2",

"description":"",

"wxlogo":"",

"bodystyleinline":,

"bg":"",

"bgtype":"",

"bottomstyleinline":,

"bottombg":"",

"bottombgtype":"",

"uuid":"aaef8dfe-256a-4559-aec9-95d1fcdcf830",

"activeitemsname":"items",

"activeimgtype":"",

"authinfo":,

],"city_list":[

],"userid":3108779,

"username":"zhangyusheng",

"email":"zhangyusheng@***.com",

"mobile":"23123",

"truename":"張昱公升",

"isemployee":true}}

}

$.tpl就是獲取tpl這個鍵key

$[0].url  就是獲取[,] 這個陣列第乙個物件的url值 也就是1

Mysql解析json字串 陣列

1 mysql解析json字串 解決方法 json extract 原欄位,json欄位名 執行sql select json extract t.result,row json extract t.result,value json extract t.result,criteria from t...

Gosn解析Json陣列

新增依賴 compile com.google.code.gson gson 2.2.4 通過將獲取的json資料解析 httputil封裝 public static string sendget string code,string time 定義bufferedreader 輸入流來讀取 ur...

解析json物件,陣列

工作中需要請求第三方介面,返回的都是json格式的字串或者json陣列 所以用到了兩個解析的方法 1.解析字串 string sr 請求的方法 workbean workbean json.parseobject sr,workbean.class 注釋 workbean 是返回json對應的字段的...