我一直在阅读尽可能多的有关同一个问题的问题和答案,但我想我的问题需要更具创造性的方法。
所以这里有一个JSON字符串:
乍一看很疯狂,但是这样想:
有一个名为propertyObjects的数组,其中包含多个父子结构对象。
在每个级别中,只能有一个对象作为父级。正如您所看到的,对象3内部有子元素。
这是我失败的地方,我不知道如何通过递归查找子对象。而且,我也不知道如何指定每个子对象的父ID!
我还尝试了使用cross apply:
但是没有机会,我不知道这些孩子会在JSON字符串中深入多少。此外,交叉应用的结果将追加列而不是行,这会导致结果中出现一个巨大的表,在这种方法中,我甚至无法考虑指定parentIDs。
这完全是失败的,有什么想法可以让所有子项都以行的形式显示在表格中?
所需表格
所以这里有一个JSON字符串:
declare @json nvarchar(max) =
'{
"propertyObjects": [{
"propertyID": 1
, "title": "foo"
, "class": ""
, "typeid": 150
, "value": "bar"
, "children": [{}]
}, {
"propertyID": 2
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{}]
}, {
"propertyID": 3
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{
"propertyID": 4
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{}]
}, {
"propertyID": 5
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{}]
}, {
"propertyID": 6
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{
"propertyID": 7
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{
"propertyID": 8
, "title": "foo"
, "class": ""
, "typeid": 128
, "value": "bar"
, "children": [{}]
}]
}]
}]
}]
}'
乍一看很疯狂,但是这样想:
有一个名为propertyObjects的数组,其中包含多个父子结构对象。
在每个级别中,只能有一个对象作为父级。正如您所看到的,对象3内部有子元素。
我想要的是将这些对象列在表格中,同时为它们指定一个parentID,因此对象4具有ID为3的父级,而对象3本身则具有ID为0的父级,因为它基本上处于顶层。
到目前为止,我尝试了一些方法,例如使用公共表达式(Common Table Expression)进行递归调用,但我失败了:
;with cte
as
(
-- anchor member definition
select p.propertyID
, 0 as parentID
, p.title
, p.typeid
, p.[value]
, p.children
from openjson(@json, '$.propertyObjects')
with (
propertyID int
, title nvarchar(100)
, typeid int
, [value] nvarchar(1000)
, children nvarchar(max) as JSON
) as p
UNION ALL
-- recursive member definition
select 0 as propertyID
, 0 as parentID
, '' as title
, 0 typeid
, '' as [value]
, '' as children
/** child should be bound to parent **/
)
select * from cte
这是我失败的地方,我不知道如何通过递归查找子对象。而且,我也不知道如何指定每个子对象的父ID!
propertyID parentID title typeid value children
----------------------------------------------------------------------------
1 0 foo 150 bar [{}]
2 0 foo 128 bar [{}]
3 0 foo 128 bar [{ "propertyID" : 4 ...
0 0 0
我还尝试了使用cross apply:
select *
from
openjson(@json, '$.propertyObjects')
with (
propertyID int
, title nvarchar(100)
, typeid int
, [value] nvarchar(1000)
, children nvarchar(max) as JSON
) as p
cross apply
openjson(p.children)
with (
propertyID int
, title nvarchar(100)
, typeid int
, [value] nvarchar(1000)
, children nvarchar(max) as JSON
) as r
但是没有机会,我不知道这些孩子会在JSON字符串中深入多少。此外,交叉应用的结果将追加列而不是行,这会导致结果中出现一个巨大的表,在这种方法中,我甚至无法考虑指定parentIDs。
这完全是失败的,有什么想法可以让所有子项都以行的形式显示在表格中?
所需表格
propertyID parentID title typeid value
--------------------------------------------------
1 0 foo 150 bar
2 0 foo 128 bar
3 0 foo 128 bar
4 3 foo 128 bar
5 3 foo 128 bar
6 3 foo 128 bar
7 6 foo 128 bar
8 7 foo 128 bar