-1

我是处理 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}
      ]}'

请教我如何从中获取记录。

4

1 回答 1

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');
于 2018-08-01T11:02:13.230 回答