【发布时间】:2019-08-24 17:36:21
【问题描述】:
我想将来自 SurveyHero 的 RESTful api 使用到 SQL Server,我有两个问题:
- 我将如何直接在 SQL SERVER 中使用需要 BasicAuth 的 RESTful API。
- 我尝试使用 C# 中的 API 并将 JSON 数据传递给 SQL Server,并尝试使用 OPENJSON() 函数解析 JSON 数据。
API返回的json如下:
{
"surveys": [{
"survey_id": 94242,
"title": "title here",
"created_on": "2018-10-19T00:05:28+00:00",
"number_of_questions": 47,
"number_of_responses": 1403
}, {
"survey_id": 125865,
"title": "title 2 here",
"created_on": "2019-03-15T00:38:11+00:00",
"number_of_questions": 45,
"number_of_responses": 9109
}]
}
现在我尝试了下面的 OPENJSON 函数:
SELECT *
FROM OPENJSON(@json)
WITH (id int 'strict $.surveys.survey_id', title nvarchar(100) '$.surveys.title', createddate datetime '$.surveys.created_on')
上述查询失败并抛出以下错误:
JSON path is not properly formatted. Unexpected character '0' is found at position 0.
但是,如果我从根目录中删除 {"surveys":[,它可以正常工作,但是当我调用 API 时,它总是只返回这种方式,有人能建议我如何正确解析 JSON 吗?
【问题讨论】:
-
如果可以使用c#,为什么不解析其中的json并将属性作为列存储在SQL Server的表中?
标签: json tsql jsonparser open-json