表和Json的相互操作

news2024/11/19 1:29: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/937188.html

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

相关文章

计网-All

路由器的功能与路由表的查看_路由器路由表_傻傻小猪哈哈的博客-CSDN博客路由基础-直连路由、静态路由与动态路由的概念_MikeVane-bb的博客-CSDN博客路由器的功能与路由表的查看_路由器路由表_傻傻小猪哈哈的博客-CSDN博客 直连路由就是路由器直接连了一个网段,他就…

在线考试系统java线上答题问答jsp源代码MySQL

本项目为前几天收费帮学妹做的一个项目,Java EE JSP项目,在工作环境中基本使用不到,但是很多学校把这个当作编程入门的项目来做,故分享出本项目供初学者参考。 一、项目描述 在线考试系统 系统有2权限:管理员、学生 …

ChatGPT 与前端技术实现制作大屏可视化

像这样的综合案例实分析,我们可以提供案例,维度与指标数据,让ChatGPT与AIGC 帮写出完整代码,并进行一个2行2列的布局设置。 数据与指令如下: 商品名称 销量 目标 完成率 可乐 479 600 79.83% 雪碧 324 600 54.00% 红茶 379 600 63.…

NoSQL数据库介绍+Redis部署

目录 一、NoSQL概述 1、数据的高并发读写 2、海量数据的高效率存储和访问 3、数据库的高扩展和高可用 二、NoSQL的类别 1、键值存储数据库 2、列存储数据库 3、文档型数据库 4、图形化数据库 三、分布式数据库中的CAP原理 1、传统的ACID 1)、A--原子性 …

webassembly001 webassembly简述

WebAssembly 官方地址:https://webassembly.org/相关历史 https://en.wikipedia.org/wiki/WebAssembly https://brendaneich.com/2015/06/from-asm-js-to-webassembly/WebAssembly(缩写为Wasm)是一种基于堆栈的虚拟机的二进制指令格式。Wasm 被设计为编…

【C语言练习】牛客题库第(一)期

📢:如果你也对机器人、人工智能感兴趣,看来我们志同道合✨ 📢:不妨浏览一下我的博客主页【https://blog.csdn.net/weixin_51244852】 📢:文章若有幸对你有帮助,可点赞 👍…

攻防演练期间一次对某企业的渗透测试

免责声明 由于传播、利用本文章说黑客所提供的信息而造成的任何直接或者间接的后果及损失,均由使用者本人负责,文章作者说黑客不为此承担任何责任,一旦造成后果请自行承担! 前言 某次攻防演练中,主办方只提供了目标…

软考:中级软件设计师:HTML

软考:中级软件设计师:HTML 提示:系列被面试官问的问题,我自己当时不会,所以下来自己复盘一下,认真学习和总结,以应对未来更多的可能性 关于互联网大厂的笔试面试,都是需要细心准备的 &#xff…

SM3(国密)在线加密--获取摘要

SM3(国密)在线加密--获取摘要

官方项目《内容示例》中Common UI部分笔记: 1.1 Activatable Widgets

本文主要面向UMG以及Common UI的初学者 文章目录 效果展示概要Activate和Deactivate可见性绑定UI动画设置Common Activatable Widget的默认焦点 效果展示 概要 这个例子非常简单,定义了13个Common Activatable Widget CommonUI_ActivatableWidgets相当于一个容器包…

【校招VIP】java语言考点之双亲委派模型

考点介绍: 双亲委派是校招面试中的高频考点之一。 双亲委派机制定义:当一个类加载器收到了类加载的请求的时候,他不会直接去加载指定的类,而是把这个请求委托给自己的父加载器去加载。只有父加载器无法加载这个类的时候,才会由当前…

系统集成项目管理工程师【中级】考证学习资料知识点整理分享——第二章《信息系统集成及服务管理》,持续更新中........

系统集成项目管理工程师(中级)考证学习资料整理分享,持续更新中........ 目 录 第二章《信息系统集成及服务管理》 一、信息系统集成及服务管理 (一)信息系统集成及服务管理的内容 (二)信息系统集成及服务管理的推进 1.实施信息系统集成及服务资质管理制度 1)…

Pico如何使用C/C++选择使用哪个I2C控制器,以及SDA和SCL针脚

本文一开始讲述了解决方案,后面是我做的笔记,用来讲述我的发现流程和探究的 Pico I2C 代码结构。 前提知识 首先要说明一点:Pico 有两个 I2C,也就是两套 SDA 和 SCL。这点你可以在针脚图中名字看出,比如下图的 Pin 4…

AI绘图(11)stable diffusion 如何写好prompt 四

在最开始我写了三篇关于prompt的,具体的大家可以跳转来去看,以下给出来链接: AI绘图(3)stable diffusion如何写好prompt 一_牧子川的博客-CSDN博客 AI绘图(4)stable diffusion如何写好prompt …

nodejs里面的event loop

1. event loop 1.1 什么是event-loop js的标准文档定义如下 https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#event_loop https://javascript.info/event-loop html的标准定义 https://html.spec.whatwg.org/multipage/webappapis.html#event-loop-proc…

CNN 02(CNN原理)

一、卷积神经网络(CNN)原理 1.1 卷积神经网络的组成 定义 卷积神经网络由一个或多个卷积层、池化层以及全连接层等组成。与其他深度学习结构相比,卷积神经网络在图像等方面能够给出更好的结果。这一模型也可以使用反向传播算法进行训练。相比较其他浅层或深度神经…

Dreamweaver软件安装包分享(附安装教程)

目录 一、软件简介 二、软件下载 一、软件简介 Dreamweaver软件是一款专业的网页开发工具,由Adobe公司开发并广泛应用于Web开发领域。它提供了一站式的网页开发解决方案,包括网页设计、网页编程、网站管理和移动应用开发等功能。 Dreamweaver软件具有…

云计算服务体系-架构真题(十四)

云计算服务体系结构SaaS、PaaS、IaaS相对应分别()。 答案。应用层、平台层、基础设施层 (2022)给定关系模式R(U,F),其中U为属性集,F是U的一组函数依赖,那么函数依赖的公理系统(Armstrong)中分解规则是指(&…

API管理测试 - 最佳实践和关键要素

什么是API管理测试? API管理测试是在软件开发和集成功能中对应用程序接口(API)进行测试和验证的过程。它涵盖了测试API的功能、性能、安全性以及与其他系统的交互。API管理测试对于确保API的正确运行和稳定性非常重要。 ​ 为什么API管理测…

39.RESTful案例

RESTful案例 准备环境 Employee.java public class Employee {private Integer id;private String lastName;private String email;//1 male, 0 femaleprivate Integer gender; } //省略get、set和构造方法EmployeeDao.java package com.atguigu.SpringMVC.dao;import com.…