数据库中的表和Json

news2024/11/15 8:28:42

目录

一、表转Json

1.使用 for json path

2.如何返回单个Json 

3.如何给返回的Json增加一个根节点呢 

4.如何给返回的Json增加上一个节点 

二、对Json基本操作

1.判断给的字符串是否是Json格式

2.从 JSON 字符串中提取标量值

3. 从 JSON 字符串中提取对象或数组

4. 更新 JSON 字符串中属性的值,并返回已更新的 JSON 字符串

三、Json转成表

 1.使用OPENJSON  WITH 

 2.多层嵌套的Json如何转成表呢 

总结


一、表转Json

1.使用 for json path

代码如下(示例):

set ROWCOUNT 2

select * from AdministrativeDivision for json path

运行结果

[
    {
        "ID":100000,
        "Name":"中国",
        "ParentId":0,
        "ShortName":"中国",
        "LevelType":0,
        "CityCode":"",
        "ZipCode":"",
        "MergerName":"中国",
        "lng":116.3683244,
        "Lat":39.915085,
        "Pinyin":"China"
    },
    {
        "ID":110000,
        "Name":"北京",
        "ParentId":100000,
        "ShortName":"北京",
        "LevelType":1,
        "CityCode":"",
        "ZipCode":"",
        "MergerName":"中国,北京",
        "lng":116.405285,
        "Lat":39.904989,
        "Pinyin":"Beijing"
    }
]

表字段为key,对应的值为Value,

 等效与 for json auto 

set ROWCOUNT 2

select * from AdministrativeDivision  for json auto

2.如何返回单个Json 

代码如下(示例):

set ROWCOUNT 1;
select  * from AdministrativeDivision for json auto

运行结果

[
    {
        "ID":100000,
        "Name":"中国",
        "ParentId":0,
        "ShortName":"中国",
        "LevelType":0,
        "CityCode":"",
        "ZipCode":"",
        "MergerName":"中国",
        "lng":116.3683244,
        "Lat":39.915085,
        "Pinyin":"China"
    }
]

返回了一个Json对象,是以数组的形式返回的,没有达到我们想要的效果

使用这个  WITHOUT_ARRAY_WRAPPER  去掉最外层[ ]中括号的包裹

代码如下(示例):

set ROWCOUNT 1;

select  * from AdministrativeDivision for json auto, WITHOUT_ARRAY_WRAPPER

运行结果

{
    "ID":100000,
    "Name":"中国",
    "ParentId":0,
    "ShortName":"中国",
    "LevelType":0,
    "CityCode":"",
    "ZipCode":"",
    "MergerName":"中国",
    "lng":116.3683244,
    "Lat":39.915085,
    "Pinyin":"China"
}

3.如何给返回的Json增加一个根节点呢 

代码如下(示例):

set ROWCOUNT 1

select * from AdministrativeDivision for json path ,root('业务信息') 

运行结果

{
    "业务信息":[
        {
            "ID":100000,
            "Name":"中国",
            "ParentId":0,
            "ShortName":"中国",
            "LevelType":0,
            "CityCode":"",
            "ZipCode":"",
            "MergerName":"中国",
            "lng":116.3683244,
            "Lat":39.915085,
            "Pinyin":"China"
        }
    ]
}

注意  root('')  和 WITHOUT_ARRAY_WRAPPER 不能同时使用

 4.如何给返回的Json增加上一个节点 

代码如下(示例):

set ROWCOUNT 0;
select a.ID, a.Name ,  b.ID as 'child.Id' ,b.Name as 'child.Name' from AdministrativeDivision  a
inner join  AdministrativeDivision b on a.ID=b.ParentId
where a.Name like '%湖北省%'  
for json path

运行结果

[
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420100,
            "Name":"武汉市"
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420200,
            "Name":"黄石市"
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420300,
            "Name":"十堰市"
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420500,
            "Name":"宜昌市"
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420600,
            "Name":"襄阳市"
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420700,
            "Name":"鄂州市"
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420800,
            "Name":"荆门市"
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420900,
            "Name":"孝感市"
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421000,
            "Name":"荆州市"
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421100,
            "Name":"黄冈市"
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421200,
            "Name":"咸宁市"
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421300,
            "Name":"随州市"
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":422800,
            "Name":"恩施土家族苗族自治州"
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":429000,
            "Name":"直辖县级"
        }
    }
]

增加多个子节点 如下

set ROWCOUNT 0;
select a.ID, a.Name ,  b.ID as 'child.Id' ,b.Name as 'child.Name',c.ID as 'child.child.Id' ,c.Name    as 'child.child.Name' 
from AdministrativeDivision  a
inner join  AdministrativeDivision b on a.ID=b.ParentId
inner join  AdministrativeDivision c on c.ParentId=b.ID
where a.Name like '%湖北省%'  
for json path

运行结果如下

[
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420100,
            "Name":"武汉市",
            "child":{
                "Id":420102,
                "Name":"江岸区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420100,
            "Name":"武汉市",
            "child":{
                "Id":420103,
                "Name":"江汉区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420100,
            "Name":"武汉市",
            "child":{
                "Id":420104,
                "Name":"硚口区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420100,
            "Name":"武汉市",
            "child":{
                "Id":420105,
                "Name":"汉阳区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420100,
            "Name":"武汉市",
            "child":{
                "Id":420106,
                "Name":"武昌区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420100,
            "Name":"武汉市",
            "child":{
                "Id":420107,
                "Name":"青山区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420100,
            "Name":"武汉市",
            "child":{
                "Id":420111,
                "Name":"洪山区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420100,
            "Name":"武汉市",
            "child":{
                "Id":420112,
                "Name":"东西湖区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420100,
            "Name":"武汉市",
            "child":{
                "Id":420113,
                "Name":"汉南区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420100,
            "Name":"武汉市",
            "child":{
                "Id":420114,
                "Name":"蔡甸区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420100,
            "Name":"武汉市",
            "child":{
                "Id":420115,
                "Name":"江夏区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420100,
            "Name":"武汉市",
            "child":{
                "Id":420116,
                "Name":"黄陂区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420100,
            "Name":"武汉市",
            "child":{
                "Id":420117,
                "Name":"新洲区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420200,
            "Name":"黄石市",
            "child":{
                "Id":420202,
                "Name":"黄石港区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420200,
            "Name":"黄石市",
            "child":{
                "Id":420203,
                "Name":"西塞山区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420200,
            "Name":"黄石市",
            "child":{
                "Id":420204,
                "Name":"下陆区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420200,
            "Name":"黄石市",
            "child":{
                "Id":420205,
                "Name":"铁山区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420200,
            "Name":"黄石市",
            "child":{
                "Id":420222,
                "Name":"阳新县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420200,
            "Name":"黄石市",
            "child":{
                "Id":420281,
                "Name":"大冶市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420300,
            "Name":"十堰市",
            "child":{
                "Id":420302,
                "Name":"茅箭区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420300,
            "Name":"十堰市",
            "child":{
                "Id":420303,
                "Name":"张湾区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420300,
            "Name":"十堰市",
            "child":{
                "Id":420304,
                "Name":"郧阳区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420300,
            "Name":"十堰市",
            "child":{
                "Id":420322,
                "Name":"郧西县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420300,
            "Name":"十堰市",
            "child":{
                "Id":420323,
                "Name":"竹山县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420300,
            "Name":"十堰市",
            "child":{
                "Id":420324,
                "Name":"竹溪县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420300,
            "Name":"十堰市",
            "child":{
                "Id":420325,
                "Name":"房县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420300,
            "Name":"十堰市",
            "child":{
                "Id":420381,
                "Name":"丹江口市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420500,
            "Name":"宜昌市",
            "child":{
                "Id":420502,
                "Name":"西陵区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420500,
            "Name":"宜昌市",
            "child":{
                "Id":420503,
                "Name":"伍家岗区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420500,
            "Name":"宜昌市",
            "child":{
                "Id":420504,
                "Name":"点军区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420500,
            "Name":"宜昌市",
            "child":{
                "Id":420505,
                "Name":"猇亭区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420500,
            "Name":"宜昌市",
            "child":{
                "Id":420506,
                "Name":"夷陵区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420500,
            "Name":"宜昌市",
            "child":{
                "Id":420525,
                "Name":"远安县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420500,
            "Name":"宜昌市",
            "child":{
                "Id":420526,
                "Name":"兴山县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420500,
            "Name":"宜昌市",
            "child":{
                "Id":420527,
                "Name":"秭归县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420500,
            "Name":"宜昌市",
            "child":{
                "Id":420528,
                "Name":"长阳土家族自治县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420500,
            "Name":"宜昌市",
            "child":{
                "Id":420529,
                "Name":"五峰土家族自治县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420500,
            "Name":"宜昌市",
            "child":{
                "Id":420581,
                "Name":"宜都市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420500,
            "Name":"宜昌市",
            "child":{
                "Id":420582,
                "Name":"当阳市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420500,
            "Name":"宜昌市",
            "child":{
                "Id":420583,
                "Name":"枝江市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420600,
            "Name":"襄阳市",
            "child":{
                "Id":420602,
                "Name":"襄城区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420600,
            "Name":"襄阳市",
            "child":{
                "Id":420606,
                "Name":"樊城区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420600,
            "Name":"襄阳市",
            "child":{
                "Id":420607,
                "Name":"襄州区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420600,
            "Name":"襄阳市",
            "child":{
                "Id":420624,
                "Name":"南漳县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420600,
            "Name":"襄阳市",
            "child":{
                "Id":420625,
                "Name":"谷城县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420600,
            "Name":"襄阳市",
            "child":{
                "Id":420626,
                "Name":"保康县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420600,
            "Name":"襄阳市",
            "child":{
                "Id":420682,
                "Name":"老河口市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420600,
            "Name":"襄阳市",
            "child":{
                "Id":420683,
                "Name":"枣阳市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420600,
            "Name":"襄阳市",
            "child":{
                "Id":420684,
                "Name":"宜城市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420700,
            "Name":"鄂州市",
            "child":{
                "Id":420702,
                "Name":"梁子湖区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420700,
            "Name":"鄂州市",
            "child":{
                "Id":420703,
                "Name":"华容区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420700,
            "Name":"鄂州市",
            "child":{
                "Id":420704,
                "Name":"鄂城区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420800,
            "Name":"荆门市",
            "child":{
                "Id":420802,
                "Name":"东宝区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420800,
            "Name":"荆门市",
            "child":{
                "Id":420804,
                "Name":"掇刀区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420800,
            "Name":"荆门市",
            "child":{
                "Id":420821,
                "Name":"京山县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420800,
            "Name":"荆门市",
            "child":{
                "Id":420822,
                "Name":"沙洋县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420800,
            "Name":"荆门市",
            "child":{
                "Id":420881,
                "Name":"钟祥市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420900,
            "Name":"孝感市",
            "child":{
                "Id":420902,
                "Name":"孝南区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420900,
            "Name":"孝感市",
            "child":{
                "Id":420921,
                "Name":"孝昌县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420900,
            "Name":"孝感市",
            "child":{
                "Id":420922,
                "Name":"大悟县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420900,
            "Name":"孝感市",
            "child":{
                "Id":420923,
                "Name":"云梦县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420900,
            "Name":"孝感市",
            "child":{
                "Id":420981,
                "Name":"应城市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420900,
            "Name":"孝感市",
            "child":{
                "Id":420982,
                "Name":"安陆市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":420900,
            "Name":"孝感市",
            "child":{
                "Id":420984,
                "Name":"汉川市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421000,
            "Name":"荆州市",
            "child":{
                "Id":421002,
                "Name":"沙市区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421000,
            "Name":"荆州市",
            "child":{
                "Id":421003,
                "Name":"荆州区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421000,
            "Name":"荆州市",
            "child":{
                "Id":421022,
                "Name":"公安县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421000,
            "Name":"荆州市",
            "child":{
                "Id":421023,
                "Name":"监利县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421000,
            "Name":"荆州市",
            "child":{
                "Id":421024,
                "Name":"江陵县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421000,
            "Name":"荆州市",
            "child":{
                "Id":421081,
                "Name":"石首市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421000,
            "Name":"荆州市",
            "child":{
                "Id":421083,
                "Name":"洪湖市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421000,
            "Name":"荆州市",
            "child":{
                "Id":421087,
                "Name":"松滋市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421100,
            "Name":"黄冈市",
            "child":{
                "Id":421102,
                "Name":"黄州区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421100,
            "Name":"黄冈市",
            "child":{
                "Id":421121,
                "Name":"团风县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421100,
            "Name":"黄冈市",
            "child":{
                "Id":421122,
                "Name":"红安县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421100,
            "Name":"黄冈市",
            "child":{
                "Id":421123,
                "Name":"罗田县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421100,
            "Name":"黄冈市",
            "child":{
                "Id":421124,
                "Name":"英山县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421100,
            "Name":"黄冈市",
            "child":{
                "Id":421125,
                "Name":"浠水县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421100,
            "Name":"黄冈市",
            "child":{
                "Id":421126,
                "Name":"蕲春县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421100,
            "Name":"黄冈市",
            "child":{
                "Id":421127,
                "Name":"黄梅县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421100,
            "Name":"黄冈市",
            "child":{
                "Id":421181,
                "Name":"麻城市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421100,
            "Name":"黄冈市",
            "child":{
                "Id":421182,
                "Name":"武穴市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421200,
            "Name":"咸宁市",
            "child":{
                "Id":421202,
                "Name":"咸安区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421200,
            "Name":"咸宁市",
            "child":{
                "Id":421221,
                "Name":"嘉鱼县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421200,
            "Name":"咸宁市",
            "child":{
                "Id":421222,
                "Name":"通城县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421200,
            "Name":"咸宁市",
            "child":{
                "Id":421223,
                "Name":"崇阳县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421200,
            "Name":"咸宁市",
            "child":{
                "Id":421224,
                "Name":"通山县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421200,
            "Name":"咸宁市",
            "child":{
                "Id":421281,
                "Name":"赤壁市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421300,
            "Name":"随州市",
            "child":{
                "Id":421303,
                "Name":"曾都区"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421300,
            "Name":"随州市",
            "child":{
                "Id":421321,
                "Name":"随县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":421300,
            "Name":"随州市",
            "child":{
                "Id":421381,
                "Name":"广水市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":422800,
            "Name":"恩施土家族苗族自治州",
            "child":{
                "Id":422801,
                "Name":"恩施市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":422800,
            "Name":"恩施土家族苗族自治州",
            "child":{
                "Id":422802,
                "Name":"利川市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":422800,
            "Name":"恩施土家族苗族自治州",
            "child":{
                "Id":422822,
                "Name":"建始县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":422800,
            "Name":"恩施土家族苗族自治州",
            "child":{
                "Id":422823,
                "Name":"巴东县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":422800,
            "Name":"恩施土家族苗族自治州",
            "child":{
                "Id":422825,
                "Name":"宣恩县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":422800,
            "Name":"恩施土家族苗族自治州",
            "child":{
                "Id":422826,
                "Name":"咸丰县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":422800,
            "Name":"恩施土家族苗族自治州",
            "child":{
                "Id":422827,
                "Name":"来凤县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":422800,
            "Name":"恩施土家族苗族自治州",
            "child":{
                "Id":422828,
                "Name":"鹤峰县"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":429000,
            "Name":"直辖县级",
            "child":{
                "Id":429004,
                "Name":"仙桃市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":429000,
            "Name":"直辖县级",
            "child":{
                "Id":429005,
                "Name":"潜江市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":429000,
            "Name":"直辖县级",
            "child":{
                "Id":429006,
                "Name":"天门市"
            }
        }
    },
    {
        "ID":420000,
        "Name":"湖北省",
        "child":{
            "Id":429000,
            "Name":"直辖县级",
            "child":{
                "Id":429021,
                "Name":"神农架林区"
            }
        }
    }
]

从结果来看 并没有达到我们想要的效果  同一个父节点,没有包含到所有的子节点, 

如何解决这种情况呢 也是有解决方法的

代码如下(示例):

set ROWCOUNT 0;

select a.ID, a.Name ,
(select b.ID,b.Name,
(select c.ID,c.Name from  AdministrativeDivision c where c.ParentId=b.ID 
for json path
) as child 
from  AdministrativeDivision b where a.ID=b.ParentId 
for json path
) as child 
from AdministrativeDivision  a
where a.Name like '%湖北省%'  
for json path

运行结果

[
    {
        "ID":420000,
        "Name":"湖北省",
        "child":[
            {
                "ID":420100,
                "Name":"武汉市",
                "child":[
                    {
                        "ID":420102,
                        "Name":"江岸区"
                    },
                    {
                        "ID":420103,
                        "Name":"江汉区"
                    },
                    {
                        "ID":420104,
                        "Name":"硚口区"
                    },
                    {
                        "ID":420105,
                        "Name":"汉阳区"
                    },
                    {
                        "ID":420106,
                        "Name":"武昌区"
                    },
                    {
                        "ID":420107,
                        "Name":"青山区"
                    },
                    {
                        "ID":420111,
                        "Name":"洪山区"
                    },
                    {
                        "ID":420112,
                        "Name":"东西湖区"
                    },
                    {
                        "ID":420113,
                        "Name":"汉南区"
                    },
                    {
                        "ID":420114,
                        "Name":"蔡甸区"
                    },
                    {
                        "ID":420115,
                        "Name":"江夏区"
                    },
                    {
                        "ID":420116,
                        "Name":"黄陂区"
                    },
                    {
                        "ID":420117,
                        "Name":"新洲区"
                    }
                ]
            },
            {
                "ID":420200,
                "Name":"黄石市",
                "child":[
                    {
                        "ID":420202,
                        "Name":"黄石港区"
                    },
                    {
                        "ID":420203,
                        "Name":"西塞山区"
                    },
                    {
                        "ID":420204,
                        "Name":"下陆区"
                    },
                    {
                        "ID":420205,
                        "Name":"铁山区"
                    },
                    {
                        "ID":420222,
                        "Name":"阳新县"
                    },
                    {
                        "ID":420281,
                        "Name":"大冶市"
                    }
                ]
            },
            {
                "ID":420300,
                "Name":"十堰市",
                "child":[
                    {
                        "ID":420302,
                        "Name":"茅箭区"
                    },
                    {
                        "ID":420303,
                        "Name":"张湾区"
                    },
                    {
                        "ID":420304,
                        "Name":"郧阳区"
                    },
                    {
                        "ID":420322,
                        "Name":"郧西县"
                    },
                    {
                        "ID":420323,
                        "Name":"竹山县"
                    },
                    {
                        "ID":420324,
                        "Name":"竹溪县"
                    },
                    {
                        "ID":420325,
                        "Name":"房县"
                    },
                    {
                        "ID":420381,
                        "Name":"丹江口市"
                    }
                ]
            },
            {
                "ID":420500,
                "Name":"宜昌市",
                "child":[
                    {
                        "ID":420502,
                        "Name":"西陵区"
                    },
                    {
                        "ID":420503,
                        "Name":"伍家岗区"
                    },
                    {
                        "ID":420504,
                        "Name":"点军区"
                    },
                    {
                        "ID":420505,
                        "Name":"猇亭区"
                    },
                    {
                        "ID":420506,
                        "Name":"夷陵区"
                    },
                    {
                        "ID":420525,
                        "Name":"远安县"
                    },
                    {
                        "ID":420526,
                        "Name":"兴山县"
                    },
                    {
                        "ID":420527,
                        "Name":"秭归县"
                    },
                    {
                        "ID":420528,
                        "Name":"长阳土家族自治县"
                    },
                    {
                        "ID":420529,
                        "Name":"五峰土家族自治县"
                    },
                    {
                        "ID":420581,
                        "Name":"宜都市"
                    },
                    {
                        "ID":420582,
                        "Name":"当阳市"
                    },
                    {
                        "ID":420583,
                        "Name":"枝江市"
                    }
                ]
            },
            {
                "ID":420600,
                "Name":"襄阳市",
                "child":[
                    {
                        "ID":420602,
                        "Name":"襄城区"
                    },
                    {
                        "ID":420606,
                        "Name":"樊城区"
                    },
                    {
                        "ID":420607,
                        "Name":"襄州区"
                    },
                    {
                        "ID":420624,
                        "Name":"南漳县"
                    },
                    {
                        "ID":420625,
                        "Name":"谷城县"
                    },
                    {
                        "ID":420626,
                        "Name":"保康县"
                    },
                    {
                        "ID":420682,
                        "Name":"老河口市"
                    },
                    {
                        "ID":420683,
                        "Name":"枣阳市"
                    },
                    {
                        "ID":420684,
                        "Name":"宜城市"
                    }
                ]
            },
            {
                "ID":420700,
                "Name":"鄂州市",
                "child":[
                    {
                        "ID":420702,
                        "Name":"梁子湖区"
                    },
                    {
                        "ID":420703,
                        "Name":"华容区"
                    },
                    {
                        "ID":420704,
                        "Name":"鄂城区"
                    }
                ]
            },
            {
                "ID":420800,
                "Name":"荆门市",
                "child":[
                    {
                        "ID":420802,
                        "Name":"东宝区"
                    },
                    {
                        "ID":420804,
                        "Name":"掇刀区"
                    },
                    {
                        "ID":420821,
                        "Name":"京山县"
                    },
                    {
                        "ID":420822,
                        "Name":"沙洋县"
                    },
                    {
                        "ID":420881,
                        "Name":"钟祥市"
                    }
                ]
            },
            {
                "ID":420900,
                "Name":"孝感市",
                "child":[
                    {
                        "ID":420902,
                        "Name":"孝南区"
                    },
                    {
                        "ID":420921,
                        "Name":"孝昌县"
                    },
                    {
                        "ID":420922,
                        "Name":"大悟县"
                    },
                    {
                        "ID":420923,
                        "Name":"云梦县"
                    },
                    {
                        "ID":420981,
                        "Name":"应城市"
                    },
                    {
                        "ID":420982,
                        "Name":"安陆市"
                    },
                    {
                        "ID":420984,
                        "Name":"汉川市"
                    }
                ]
            },
            {
                "ID":421000,
                "Name":"荆州市",
                "child":[
                    {
                        "ID":421002,
                        "Name":"沙市区"
                    },
                    {
                        "ID":421003,
                        "Name":"荆州区"
                    },
                    {
                        "ID":421022,
                        "Name":"公安县"
                    },
                    {
                        "ID":421023,
                        "Name":"监利县"
                    },
                    {
                        "ID":421024,
                        "Name":"江陵县"
                    },
                    {
                        "ID":421081,
                        "Name":"石首市"
                    },
                    {
                        "ID":421083,
                        "Name":"洪湖市"
                    },
                    {
                        "ID":421087,
                        "Name":"松滋市"
                    }
                ]
            },
            {
                "ID":421100,
                "Name":"黄冈市",
                "child":[
                    {
                        "ID":421102,
                        "Name":"黄州区"
                    },
                    {
                        "ID":421121,
                        "Name":"团风县"
                    },
                    {
                        "ID":421122,
                        "Name":"红安县"
                    },
                    {
                        "ID":421123,
                        "Name":"罗田县"
                    },
                    {
                        "ID":421124,
                        "Name":"英山县"
                    },
                    {
                        "ID":421125,
                        "Name":"浠水县"
                    },
                    {
                        "ID":421126,
                        "Name":"蕲春县"
                    },
                    {
                        "ID":421127,
                        "Name":"黄梅县"
                    },
                    {
                        "ID":421181,
                        "Name":"麻城市"
                    },
                    {
                        "ID":421182,
                        "Name":"武穴市"
                    }
                ]
            },
            {
                "ID":421200,
                "Name":"咸宁市",
                "child":[
                    {
                        "ID":421202,
                        "Name":"咸安区"
                    },
                    {
                        "ID":421221,
                        "Name":"嘉鱼县"
                    },
                    {
                        "ID":421222,
                        "Name":"通城县"
                    },
                    {
                        "ID":421223,
                        "Name":"崇阳县"
                    },
                    {
                        "ID":421224,
                        "Name":"通山县"
                    },
                    {
                        "ID":421281,
                        "Name":"赤壁市"
                    }
                ]
            },
            {
                "ID":421300,
                "Name":"随州市",
                "child":[
                    {
                        "ID":421303,
                        "Name":"曾都区"
                    },
                    {
                        "ID":421321,
                        "Name":"随县"
                    },
                    {
                        "ID":421381,
                        "Name":"广水市"
                    }
                ]
            },
            {
                "ID":422800,
                "Name":"恩施土家族苗族自治州",
                "child":[
                    {
                        "ID":422801,
                        "Name":"恩施市"
                    },
                    {
                        "ID":422802,
                        "Name":"利川市"
                    },
                    {
                        "ID":422822,
                        "Name":"建始县"
                    },
                    {
                        "ID":422823,
                        "Name":"巴东县"
                    },
                    {
                        "ID":422825,
                        "Name":"宣恩县"
                    },
                    {
                        "ID":422826,
                        "Name":"咸丰县"
                    },
                    {
                        "ID":422827,
                        "Name":"来凤县"
                    },
                    {
                        "ID":422828,
                        "Name":"鹤峰县"
                    }
                ]
            },
            {
                "ID":429000,
                "Name":"直辖县级",
                "child":[
                    {
                        "ID":429004,
                        "Name":"仙桃市"
                    },
                    {
                        "ID":429005,
                        "Name":"潜江市"
                    },
                    {
                        "ID":429006,
                        "Name":"天门市"
                    },
                    {
                        "ID":429021,
                        "Name":"神农架林区"
                    }
                ]
            }
        ]
    }
]

二、对Json基本操作

 先给一段json字符串的代码

DECLARE @json NVARCHAR(MAX);

SET @json = N'[
    {
        "ID":420000,
        "Name":"湖北省",
        "child":[
            {
                "ID":420100,
                "Name":"武汉市"
            },
            {
                "ID":420200,
                "Name":"黄石市"
            },
            {
                "ID":420300,
                "Name":"十堰市"
            },
            {
                "ID":420500,
                "Name":"宜昌市"
            },
            {
                "ID":420600,
                "Name":"襄阳市"
            },
            {
                "ID":420700,
                "Name":"鄂州市"
            },
            {
                "ID":420800,
                "Name":"荆门市"
            },
            {
                "ID":420900,
                "Name":"孝感市"
            },
            {
                "ID":421000,
                "Name":"荆州市"
            },
            {
                "ID":421100,
                "Name":"黄冈市"
            },
            {
                "ID":421200,
                "Name":"咸宁市"
            },
            {
                "ID":421300,
                "Name":"随州市"
            },
            {
                "ID":422800,
                "Name":"恩施土家族苗族自治州"
            },
            {
                "ID":429000,
                "Name":"直辖县级"
            }
        ]
    }
]';
declare @t table (
temp nvarchar(max)
)
insert into @t
select @json

1.判断给的字符串是否是Json格式

代码如下(示例):

select isjson(temp) from   @t 

运行结果 

 

 返回1表示为json格式 返回0则不是

2.从 JSON 字符串中提取标量值

获取湖北省的ID 

代码如下(示例):

select JSON_VALUE(temp,'$[0].ID') from   @t 

运行结果


3. 从 JSON 字符串中提取对象或数组

获取黄石的节点 

代码如下(示例):

select JSON_QUERY(temp ,'$[0].child[1]' ) from   @t 

运行结果

4. 更新 JSON 字符串中属性的值,并返回已更新的 JSON 字符串

把湖北省的ID420000 改成 420001

代码如下(示例):


select JSON_MODIFY(temp,'$[0].ID','420001') from   @t 

运行结果

三、Json转成表

 1.使用OPENJSON  WITH 

代码如下(示例):


DECLARE @json NVARCHAR(MAX);

SET @json = N'[
    {
        "ID":420000,
        "Name":"湖北省"
	}      
]';



SELECT *
FROM OPENJSON(@json)
WITH (
    ID INT 'strict  $.ID',
    Name NVARCHAR(50) '$.Name'
    )

运行结果

 2.多层嵌套的Json如何转成表呢 

代码如下(示例):

DECLARE @json NVARCHAR(MAX);

SET @json = N'
    {
        "ID":420000,
        "Name":"湖北省",
        "child":[
            {
                "ID":420100,
                "Name":"武汉市"
            },
            {
                "ID":420200,
                "Name":"黄石市"
            },
            {
                "ID":420300,
                "Name":"十堰市"
            },
            {
                "ID":420500,
                "Name":"宜昌市"
            },
            {
                "ID":420600,
                "Name":"襄阳市"
            },
            {
                "ID":420700,
                "Name":"鄂州市"
            },
            {
                "ID":420800,
                "Name":"荆门市"
            },
            {
                "ID":420900,
                "Name":"孝感市"
            },
            {
                "ID":421000,
                "Name":"荆州市"
            },
            {
                "ID":421100,
                "Name":"黄冈市"
            },
            {
                "ID":421200,
                "Name":"咸宁市"
            },
            {
                "ID":421300,
                "Name":"随州市"
            },
            {
                "ID":422800,
                "Name":"恩施土家族苗族自治州"
            },
            {
                "ID":429000,
                "Name":"直辖县级"
            }
        ]
    }
';



SELECT ID,Name,ParentId,ParentName
FROM OPENJSON(@json)
WITH (
    ParentId INT 'strict  $.ID',
    ParentName NVARCHAR(50) '$.Name',
    Names NVARCHAR(max) '$.child' AS JSON
    )
OUTER APPLY OPENJSON(Names) WITH (ID int '$.ID' , Name NVARCHAR(50) '$.Name');
	

运行结果

OUTER APPLY OPENJSON 将第一级实体与子数组联接,并返回平展的结果集。

总结

表和Json是可以做到相互转化的

表转json 使用for json path,json 转表 使用OPENJSON  WITH 

多层的json转表 OUTER APPLY OPENJSON

注意数据库对json的操作,serversql数据库的版本支持2016及以上

以上我是的总结内容,要想了解更多相关知识 ,查阅官方文档

在 SQL Server 中使用 JSON 数据 - SQL Server | Microsoft Learn

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/958776.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

价格战“杀疯了”?「智驾」系统级降本增效,才是更优解

从3000元到1500元,再到千元级别,今年以来,行泊一体域控产品不断刷新降本底线。 以上,也反映出今年中国智驾规模量产赛道的竞争激烈程度。当前,各路 Tier1甚至Tier2供应商们还在加速“内卷”,从早期的卷技术…

7个公认的wordpress外贸独立站优点

随着全球化进程的加快,越来越多的企业开始将业务拓展到国际市场。对于外贸企业而言,拥有一个专业且易于管理的网站非常重要。WordPress外贸独立站恰好满足了这一需求,它不仅具备开源、可定制等特点,还有以下几个优点:​…

idea中设置style固定样式

一、样式设置首先打开IDEA之后,点击任务栏的“File”→Settings 二、设置style行内样式 1.首先打开IDEA之后,点击任务栏的“File”。 2.在下拉列表中中选择“Settings” 3.在弹出的设置页面中找到Editor-LiveTemplates 点击号,先选中Template Group...创建 三、详…

【01】弄懂共识机制PoW

基于工作量证明机制的共识机制PoW(Proof of Work) 特点就是多劳多特 共识过程 一个区块链系统中,交易历经多个步骤才能得以上链,并且需要经过多个节点的验证。以下是这些步骤的详细叙述: 交易进入交易池(内…

VS2022 C语言课程设计学生成绩管理系统

C语言课程设计题目及要求 学生成绩管理系统 此成绩管理系统主要利用单链表或者结构数组实现(最好用单链表实现),具有如下的五大功能模块。学生成绩管理系统功能模块图如图1所示。 说明: 1.输入记录模块 从键盘逐个输入学生记录…

leetcode236. 二叉树的最近公共祖先(java)

二叉树的最近公共祖先 题目描述递归法代码演示 上期经典 题目描述 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q …

次高端白酒:一场冰火两重的「战局」

【潮汐商业评论/ 原创】 夜深人静,Andy突然想起还没有给爷爷准备生日礼物。 “送酒肯定能让他老人家开心,但是买什么品牌呢?茅台太贵,其他好酒还真不熟。”,对于白酒研究不多的Andy来说,如何挑选到一款称…

国产ETLCloud VS 开源Kettle ETL对比分析

ETLCloud VS Kettle ETLCloud和kettle是目前国内使用最广泛的两款免费ETL工具,本文将从多个角色对ETLCloud和kettle进行对比,方便用户快速了解到两款产品的差异并根据自已的需求选择相应的工具。 ETLCloud提供了对kettle流程的迁移功能,所以…

Linux系统下建立Socket聊天服务器

目录 1.服务器结构 2.各模块函数 2.1 socket函数 2.2 bind函数 2.3 Listen函数 2.4 accept函数 2.5 接收发送函数 2.6 close函数 2.7 connect函数 3 代码段 3.1 服务器代码 1.服务器结构 使用socket的API函数编写服务端和客户端程序的步骤图示: 2.各模块函数 服务…

jmeter setUp Thread Group

SetUp Thread Group 是一种特殊类型的线程组,它用于在主测试计划执行之前执行一些初始化任务。 SetUp Thread Group 通常用于以下几种情况: 用户登录:在模拟用户执行实际测试之前,模拟用户登录到系统以获取访问权限。 创建会话&a…

STM32 RTC实验

RTC时钟简介 STM32F103的实时时钟(RTC)是一个独立的定时器。 STM32的RTC模块拥有一组连续计数的计数器,在相对应的软件配置下,可提供时钟日历的功能。 修改计数器的值可以重新设置系统的当前时间和日期。 RTC模块和时钟配置系统…

设计模式之适配器与装饰器

目录 适配器模式 简介 角色 使用 优缺点 使用场景 装饰器模式 简介 优缺点 模式结构 使用 使用场景 适配器模式 简介 允许将不兼容的对象包装成一个适配器类,使得其他类可以通过适配器类与原始对象进行交互,从而提高兼容性 角色 目标角色…

np.numpy, np.reshape, np.cumsum方法速查

1 np.numpy() 创建一个数组 state[[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]] state2np.array(state) print(state) print(state2)[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]] [[ 1 2 3 4 5] [ 6 7 8 9 10] [11 12 13 14 15]] 2 np.reshape() 常用于矩阵规…

向日葵光伏发电

“光伏向日葵”是一个可移动、可调节的光伏储能系统。系统使用全球定位跟踪算法,使叶片(光伏板)跟随太阳移动的轨迹自动调节叶片方向、角度及收放状态,保证了叶片与太阳始终保持着最佳的发电角度,实现全时段发电效率最…

Acrobat Pro DC 2023 for Mac

Acrobat Pro DC是一款由Adobe开发的专业级PDF编辑和管理软件。作为PDF行业的标准工具,它提供了广泛的功能和工具,适用于个人用户、企业和专业人士。 Acrobat Pro DC具备丰富的编辑功能,可以对PDF文件进行文本编辑、图像编辑和页面重排等操作。…

大数据计算引擎 EasyMR 如何简单高效管理 Yarn 资源队列

设想一下,作为一个开发人员,你现在所在的公司有一套线上的 Hadoop 集群。A部门经常做一些定时的 BI 报表,B部门则经常使用软件做一些临时需求。那么他们肯定会遇到同时提交任务的场景,这个时候到底应该如何分配资源满足这两个任务…

OB Cloud上新,4.1版本现已全面开放

2022 年 8 月 10 日,OceanBase 宣布 OceanBase 公有云服务全球开服,帮助不同规模客户,在全球不同区域,享受同样优质的企业级数据库产品与服务。 经过近一年的发展,公有云业务取得了长足的发展,去年对客收入…

截取字符串 substr lastIndexOf

效果图 代码 item.content.substr(item.content.lastIndexOf(/) 1, item.content.length - item.content.lastIndexOf(/)) 就可以得到

linux系统维护篇:/dev/null 妙用

介绍 /dev/null 是一个特殊的设备文件,该文件接收的所有数据都会被丢弃(不显示、不打印、不记录),类似于黑洞。 场景 场景1,执行命令有输出信息(正确和错误),且直接通过输出内容判断…

python -- 实现路径的匹配,剔除掉指定路径,并保存路径

python – 实现路径的匹配,剔除掉指定路径,并保存路径 在处理nc数据时,由于部分数据在插值的过程中,存在过多的0值,使得在制作标签时该时刻的数据出现报错,但是对于一年的数据量来说,无关紧要&…