C# Restsharp从内容中获取特定的JSON值
我试图从我的内容中获得一个特定的值,但我不知道如何才能做到这一点 我使用RestSharp(C#)来运行JSON代码,但当我执行命令时,它返回一个错误。我需要从属性errorMessage中获取值C# Restsharp从内容中获取特定的JSON值,c#,json,restsharp,C#,Json,Restsharp,我试图从我的内容中获得一个特定的值,但我不知道如何才能做到这一点 我使用RestSharp(C#)来运行JSON代码,但当我执行命令时,它返回一个错误。我需要从属性errorMessage中获取值 var json = JsonConvert.SerializeObject(objectToUpdate); var request = new RestRequest(Method.POST); request.RequestFormat = DataFormat.Json; request.A
var json = JsonConvert.SerializeObject(objectToUpdate);
var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddCookie("authToken", token);
request.AddParameter("application/json", json, ParameterType.RequestBody);
var response = client.Execute<T>(request);
After execute this code my response return the below JSON:
{
"response":{},
"status":{
"success":false,
"detail":{
"errormessage":[{
"fieldName":"departmentCode",
"errorMessage":"Department code provided is already associated with another department",
"errorCode":"DUPLICATE_DEPARTMENT_CODE"
}],
"error":"Validation failure on request",
"operation":"internal",
"errorcode":"412"
}
}
}
var json=JsonConvert.SerializeObject(objectToUpdate);
var请求=新的重新请求(Method.POST);
request.RequestFormat=DataFormat.Json;
AddCookie(“authToken”,token);
AddParameter(“application/json”,json,ParameterType.RequestBody);
var response=client.Execute(请求);
执行此代码后,我的响应返回以下JSON:
{
“答复”:{},
“地位”:{
“成功”:错误,
“细节”:{
“错误消息”:[{
“字段名”:“部门代码”,
“errorMessage”:“提供的部门代码已与其他部门关联”,
“错误代码”:“重复的部门代码”
}],
“错误”:“请求验证失败”,
“操作”:“内部”,
“错误代码”:“412”
}
}
}
您可以有一个类,它代表您期望的响应:
class ApiResponse{
// use a class that represents normal response instead of object
// if you need to interact with it.
public object Response {get; set;}
public ResponseStatus Status{get; set;}
}
class ResponseStatus {
public StatusDetail Detail{get; set;}
public bool Success {get; set;}
}
class StatusDetail {
public ErrorMessage[] ErrorMessage{get; set;}
}
class ErrorMessage{
public string FieldName{get; set;}
public string ErrorMessage{get; set;}
public string ErrorCode{get; set;}
}
然后,您可以从RestSharp客户端获得解析响应:
var response = client.Execute<ApiResponse>(request);
var message = response.Data.Response.Detail.ErrorMessage.First().ErrorMessage;
var response=client.Execute)。您可以有一个类,它表示您期望的响应:
class ApiResponse{
// use a class that represents normal response instead of object
// if you need to interact with it.
public object Response {get; set;}
public ResponseStatus Status{get; set;}
}
class ResponseStatus {
public StatusDetail Detail{get; set;}
public bool Success {get; set;}
}
class StatusDetail {
public ErrorMessage[] ErrorMessage{get; set;}
}
class ErrorMessage{
public string FieldName{get; set;}
public string ErrorMessage{get; set;}
public string ErrorCode{get; set;}
}
然后,您可以从RestSharp客户端获得解析响应:
var response = client.Execute<ApiResponse>(request);
var message = response.Data.Response.Detail.ErrorMessage.First().ErrorMessage;
var response=client.Execute)。在“request.AddCookie”(“authToken”,token);”之前尝试“request.Parameters.Clear();”可能会看一看。其他原因-错误消息显示您的消息未通过验证-您在departmentCodeHello@DarkoMaricProgramer中存在内容问题,我对参数没有问题,我只需要从errorMessage中获取信息以保存在日志中。我明白了…然后尝试:dynamic result=JObject.Parse(response);var detail=result.status.detail.errormessage;foreach(var项详细信息){};在“request.AddCookie”(“authToken”,token);”之前尝试“request.Parameters.Clear();”可能会看一看。其他原因-错误消息显示您的消息未通过验证-您在departmentCodeHello@DarkoMaricProgramer中存在内容问题,我对参数没有问题,我只需要从errorMessage中获取信息以保存在日志中。我明白了…然后尝试:dynamic result=JObject.Parse(response);var detail=result.status.detail.errormessage;foreach(var项详细信息){};谢谢大家。我用了Justinas说的例子。。。帕夫。。。。我构建了类的结构,代码运行正常。。。。ApiResponse ApiResponse=新的ApiResponse();apiResponse.Response=JsonConvert.DeserializeObject(JObject.Parse(Response.Content)[“status”].ToString());谢谢大家。我用了Justinas说的例子。。。帕夫。。。。我构建了类的结构,代码运行正常。。。。ApiResponse ApiResponse=新的ApiResponse();apiResponse.Response=JsonConvert.DeserializeObject(JObject.Parse(Response.Content)[“status”].ToString());