我需要将JSON作为字符串传递给存储过程,然后使用OPENJSON转换该字符串JSON。输入参数必须为字符串格式,并且包含\ scape字符。
开放式JSON在SQL Server 2016中可用,我尝试使用不带'\'的OPENJSON,它非常正常
此代码有效:
<?php
if ($chauffeur_data['enable-paypal'] == '1') {
$paypal_check = '1';
$stripe_check = '0';
$cash_check = '0';
$pos_check = '0';
} elseif ($chauffeur_data['enable-paypal'] == '0' && $chauffeur_data['enable-stripe'] == '1') {
$paypal_check = '0';
$stripe_check = '1';
$cash_check = '0';
$pos_check = '0';
} elseif ($chauffeur_data['enable-stripe'] == '0' && $chauffeur_data['enable-cash'] == '1') {
$paypal_check = '0';
$stripe_check = '0';
$cash_check = '1';
$pos_check = '0';
} else {
$paypal_check = '0';
$stripe_check = '0';
$cash_check = '0';
$pos_check = '1';
}
这不起作用:
DECLARE @test1 NVARCHAR(MAX)='{"name":"john","age":22,"class":"mca"}'
SELECT * FROM OPENJSON(@test1)
答案 0 :(得分:1)
但是,不是最佳方法:
DECLARE @test2 NVARCHAR(MAX) = '{\"name\":\"john smith\",\"age\":22,\"class\":\"mca\"}';
SELECT j.*
FROM OPENJSON('{"t":"' + @test2 + '"}') t
CROSS APPLY OPENJSON(t.[value]) j
;
另一种解决方案:
SELECT *
FROM OPENJSON(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@test2,'\\t','\t'),'\\r','\r'),'\\n','\n'),'\\f','\f'),'\\b','\b'),'\"','"'),'\\','\')) t
;