我是处理json记录的新手。我有一个JSON记录,如下所示:
{"responseobj":{"ResponseCode":"0","ResponseDesc":"Success"},
"objDropdown":[{"Master_id":0,"ID":"en","Value":"English","Selected":false,"IsActive":false,"Description":null,"TopupId":0,"TopupAmount":0}
,{"Master_id":0,"ID":"fr","Value":"français","Selected":false,"IsActive":false,"Description":null,"TopupId":0,"TopupAmount":0}
,{"Master_id":0,"ID":"it","Value":"italiano","Selected":false,"IsActive":false,"Description":null,"TopupId":0,"TopupAmount":0}
,{"Master_id":0,"ID":"pt","Value":"Português","Selected":false,"IsActive":false,"Description":null,"TopupId":0,"TopupAmount":0}
,{"Master_id":0,"ID":"es","Value":"español","Selected":false,"IsActive":false,"Description":null,"TopupId":0,"TopupAmount":0}
,{"Master_id":0,"ID":"de","Value":"Deutsch","Selected":false,"IsActive":false,"Description":null,"TopupId":0,"TopupAmount":0}
,{"Master_id":0,"ID":"nl","Value":"Nederlands","Selected":false,"IsActive":false,"Description":null,"TopupId":0,"TopupAmount":0}
]}'
请教我如何从中获取记录。
答案 0 :(得分:1)
您可以使用OpenJSON()-SQL Server 2016及更高版本:
DECLARE @json VARCHAR(4000);
SET @json
= '{"responseobj":{"ResponseCode":"0","ResponseDesc":"Success"},
"objDropdown":[{"Master_id":0,"ID":"en","Value":"English","Selected":false,"IsActive":false,"Description":null,"TopupId":0,"TopupAmount":0}
,{"Master_id":0,"ID":"fr","Value":"français","Selected":false,"IsActive":false,"Description":null,"TopupId":0,"TopupAmount":0}
,{"Master_id":0,"ID":"it","Value":"italiano","Selected":false,"IsActive":false,"Description":null,"TopupId":0,"TopupAmount":0}
,{"Master_id":0,"ID":"pt","Value":"Português","Selected":false,"IsActive":false,"Description":null,"TopupId":0,"TopupAmount":0}
,{"Master_id":0,"ID":"es","Value":"español","Selected":false,"IsActive":false,"Description":null,"TopupId":0,"TopupAmount":0}
,{"Master_id":0,"ID":"de","Value":"Deutsch","Selected":false,"IsActive":false,"Description":null,"TopupId":0,"TopupAmount":0}
,{"Master_id":0,"ID":"nl","Value":"Nederlands","Selected":false,"IsActive":false,"Description":null,"TopupId":0,"TopupAmount":0}
]}';
SELECT *
FROM OPENJSON(@json, '$.objDropdown')
WITH
(
Master_id INT '$.Master_id',
ID VARCHAR(5) '$.ID',
[Value] NVARCHAR(50) '$.Value',
Selected BIT '$.Selected',
IsActive BIT '$.IsActive',
[Description] NVARCHAR(4000) '$.Description',
TopupId INT '$.TopupId',
TopupAmount INT '$.TopupAmount'
);
编辑:获取responseobj更简单:
SELECT *
FROM OPENJSON(@json, '$.responseobj');