如何在发送 GET 请求时从响应体中的 Json 中获取对象的值?
How to get access to the value of object from the Json in response body when GET request is sent?
我正在尝试从存储过程传递 JSON。所以我将它作为字符串传递,实体 class 包含从存储过程中捕获此字符串的字符串数据类型。
存储过程:
CREATE PROCEDURE [dbo].[CreateJson]
@AppointmentID int
AS
DECLARE @JsonOutput AS nvarchar(max)
BEGIN
SET NOCOUNT ON;
SET @JsonOutput = (SELECT TOP 1 AppointmentID, ReturnCode
FROM appointments
WHERE appointmentID = @AppointmentID
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)
SELECT @JsonOutput AS Details
END
实体class:
public partial class sp_test
{
public string Details { get; set; }
}
API 控制器:
[HttpGet]
public async Task<ActionResult<IEnumerable<sp_test>>> Getsp_test(int AppointmentID)
{
string StoredJson = "exec CreateJson " +
"@AppointmentID = " + AppointmentID;
var result = await _context.sp_test.FromSqlRaw(StoredJson).ToListAsync();
return result;
}
响应正文:
[
{
"details": "{\"AppointmentID\":1,\"ReturnCode\":1}"
}
]
我想获取显示对应于详细信息的 JSON。我想使用那个 JSON 作为参数来调用另一个 API。我无法访问 JSON.
有人可以帮帮我吗?
我正在使用此代码获取数据
var appointemntId= ...from your code
using (var client = new HttpClient())
{
var baseAddress = "http://...";
var api = "/< controllerName >/Getsp_test?AppointmentId="+appointmentId.ToString();
client.BaseAddress = new Uri(baseAddress);
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
client.DefaultRequestHeaders.Accept.Add(contentType);
var response = await client.GetAsync(api);
var statusCode = response.StatusCode.ToString();
if (response.IsSuccessStatusCode)
{
var json= await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<List<sp_test>>(json);
}
}
我正在尝试从存储过程传递 JSON。所以我将它作为字符串传递,实体 class 包含从存储过程中捕获此字符串的字符串数据类型。
存储过程:
CREATE PROCEDURE [dbo].[CreateJson]
@AppointmentID int
AS
DECLARE @JsonOutput AS nvarchar(max)
BEGIN
SET NOCOUNT ON;
SET @JsonOutput = (SELECT TOP 1 AppointmentID, ReturnCode
FROM appointments
WHERE appointmentID = @AppointmentID
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)
SELECT @JsonOutput AS Details
END
实体class:
public partial class sp_test
{
public string Details { get; set; }
}
API 控制器:
[HttpGet]
public async Task<ActionResult<IEnumerable<sp_test>>> Getsp_test(int AppointmentID)
{
string StoredJson = "exec CreateJson " +
"@AppointmentID = " + AppointmentID;
var result = await _context.sp_test.FromSqlRaw(StoredJson).ToListAsync();
return result;
}
响应正文:
[
{
"details": "{\"AppointmentID\":1,\"ReturnCode\":1}"
}
]
我想获取显示对应于详细信息的 JSON。我想使用那个 JSON 作为参数来调用另一个 API。我无法访问 JSON.
有人可以帮帮我吗?
我正在使用此代码获取数据
var appointemntId= ...from your code
using (var client = new HttpClient())
{
var baseAddress = "http://...";
var api = "/< controllerName >/Getsp_test?AppointmentId="+appointmentId.ToString();
client.BaseAddress = new Uri(baseAddress);
var contentType = new MediaTypeWithQualityHeaderValue("application/json");
client.DefaultRequestHeaders.Accept.Add(contentType);
var response = await client.GetAsync(api);
var statusCode = response.StatusCode.ToString();
if (response.IsSuccessStatusCode)
{
var json= await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<List<sp_test>>(json);
}
}