我正在尝试从 GET 调用中获取响应并通过 将其加载到 SQL Server 中OPENJSON
,但是我在解析响应时遇到了问题。
DECLARE @json NVARCHAR(MAX);
SET @json = N'{
"LookupServiceType": "GetAssetValues",
"Items": [
{
"id": "19676",
"value": "{\"AssetCode\":\"TDAACC\",\"Symbol\":null,\"Issue\":\"ACCOUNT #49\",\"Issuer\":\"TD AMERITRADE\"}"
},
{
"id": "19677",
"value": "{\"AssetCode\":\"RE100\",\"Symbol\":null,\"Issue\":\"APN: 057\",\"Issuer\":\"SAN ANTONIO TX 78212\"}"
},
{
"id": "19908",
"value": "{\"AssetCode\":\"NALIPO\",\"Symbol\":null,\"Issue\":\"POLICY # L0472\",\"Issuer\":\"NATIONWIDE LIFE\"}"
}
]
}';
我已经尝试了很多变化,但我就是无法做到正确。这是我尝试过的几件事。任何建议,将不胜感激。
SELECT *
FROM OPENJSON(@json, '$.Items')
WITH (
Items int '$.id',
value NVARCHAR(MAX) '$.value',
AssetCode NVARCHAR(50) '$.value.AssetCode',
Symbol NVARCHAR(50) '$.value.Symbol',
Issue NVARCHAR(50) '$.value.Issue',
Issuer NVARCHAR(50) '$.value.Issuer'
);
SELECT id,AssetCode,Symbol,Issue, Issuer
FROM OPENJSON(@json)
WITH (
Items NVARCHAR(MAX) '$.Items' AS JSON
)
OUTER APPLY OPENJSON(Items)
WITH (
id INT 'strict $.id',
value NVARCHAR(MAX) '$.value' AS JSON
)
OUTER APPLY OPENJSON(value)
WITH (
AssetCode NVARCHAR(50) '$.AssetCode',
Symbol NVARCHAR(50) '$.Symbol',
Issue NVARCHAR(50) '$.Issue',
Issuer NVARCHAR(50) '$.Issuer'
);