反序列化 json 响应
Deserialize json response
我正在使用 API,除此之外还有以下两个响应:
{
"enrollDeviceErrorResponse": {
"errorCode": "GRX-1056",
"errorMessage": "DEP Reseller ID missing.
Enter a valid DEP Reseller ID and resubmit your request."
}
}
或
{
"enrollDeviceErrorResponse": [
{
"errorCode": "GRX-1056",
"errorMessage": "DEP Reseller ID missing.
Enter a valid DEP Reseller ID and resubmit your request."
},
{
"errorCode": "DEP-ERR-3003",
"errorMessage": "Order information missing. T
he transaction needs to have one or more valid orders.
Enter valid orders and resubmit your request."
},
{
"errorCode": "DEP-ERR-3001",
"errorMessage": "Transaction ID missing.
Enter a valid transaction ID and resubmit your request."
}
]
}
我创建了一些 类 可以反序列化为这些响应:
public class EnrollDeviceErrorRoot
{
public EnrollDeviceErrorRoot()
{
this.EnrollDeviceErrorResponse = new EnrollDeviceErrorResponse();
}
public EnrollDeviceErrorResponse EnrollDeviceErrorResponse { get; set; }
}
public class EnrollDeviceErrorRoot
{
public EnrollDeviceErrorRoot()
{
this.EnrollDeviceErrorResponse = new EnrollDeviceErrorResponse();
}
public EnrollDeviceErrorResponse EnrollDeviceErrorResponse { get; set; }
}
public class EnrollDeviceErrorResponse
{
public string ErrorCode { get; set; }
public string ErrorMessage { get; set; }
}
我很难想出一个好方法来确定我应该根据响应使用 类 中的哪一个。我目前有这个失败的代码:
var multipleErrors = JsonConvert.DeserializeObject<EnrollDeviceErrorsRoot>(response);
if (multipleErrors.EnrollDeviceErrorResponse != null && multipleErrors.EnrollDeviceErrorResponse.Any())
{
this.StatusCode = multipleErrors.EnrollDeviceErrorResponse.Select(x => x.ErrorCode).Aggregate((a, b) => a + ", " + b);
this.StatusMessage = multipleErrors.EnrollDeviceErrorResponse.Select(x => x.ErrorMessage).Aggregate((a, b) => a + Environment.NewLine + b);
this.HasErrors = true;
return;
}
var singleError = JsonConvert.DeserializeObject<EnrollDeviceErrorRoot>(response);
if (!string.IsNullOrEmpty(singleError.EnrollDeviceErrorResponse.ErrorCode))
{
this.StatusCode = singleError.EnrollDeviceErrorResponse.ErrorCode;
this.StatusMessage = singleError.EnrollDeviceErrorResponse.ErrorMessage;
this.HasErrors = true;
return;
}
以上代码错误:
Newtonsoft.Json.JsonSerializationException : Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Atea.Dep.Core.Service.Models.EnrollDeviceErrorResponse]'
because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer,
not a collection type like an array or List<T>) that can be deserialized from a JSON object.
JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
当其他人不知道他们从服务器返回什么时,他们如何处理来自响应的反序列化?我当然可以只查看原始响应并确定这种方式,但是还有其他更清洁的方法可以做到这一点吗?我无法更改 API,我不知道为什么不能对包含一个或多个错误的列表做出回应。
您可以使用
JObject.Parse(response)["enrollDeviceErrorResponse"].Type
确定响应的类型。在第一种情况下,它将是对象,在第二种情况下 - 数组。然后继续适当的反序列化将很容易。
你可以使用这样的东西:
var output;
dynamic jObject = JsonConvert.DeserializeObject (outputAPI);
bool isArray = jObj.enrollDeviceErrorResponse.Type == JTokenType.Array;
bool isObject = jObj.enrollDeviceErrorResponse.Type == JTokenType.Object;
if(isObject)
output = JsonConvert.DeserializeObject<EnrollDeviceErrorResponse>(outputAPI);
//else you will use the other class to deserialize the object
我正在使用 API,除此之外还有以下两个响应:
{
"enrollDeviceErrorResponse": {
"errorCode": "GRX-1056",
"errorMessage": "DEP Reseller ID missing.
Enter a valid DEP Reseller ID and resubmit your request."
}
}
或
{
"enrollDeviceErrorResponse": [
{
"errorCode": "GRX-1056",
"errorMessage": "DEP Reseller ID missing.
Enter a valid DEP Reseller ID and resubmit your request."
},
{
"errorCode": "DEP-ERR-3003",
"errorMessage": "Order information missing. T
he transaction needs to have one or more valid orders.
Enter valid orders and resubmit your request."
},
{
"errorCode": "DEP-ERR-3001",
"errorMessage": "Transaction ID missing.
Enter a valid transaction ID and resubmit your request."
}
]
}
我创建了一些 类 可以反序列化为这些响应:
public class EnrollDeviceErrorRoot
{
public EnrollDeviceErrorRoot()
{
this.EnrollDeviceErrorResponse = new EnrollDeviceErrorResponse();
}
public EnrollDeviceErrorResponse EnrollDeviceErrorResponse { get; set; }
}
public class EnrollDeviceErrorRoot
{
public EnrollDeviceErrorRoot()
{
this.EnrollDeviceErrorResponse = new EnrollDeviceErrorResponse();
}
public EnrollDeviceErrorResponse EnrollDeviceErrorResponse { get; set; }
}
public class EnrollDeviceErrorResponse
{
public string ErrorCode { get; set; }
public string ErrorMessage { get; set; }
}
我很难想出一个好方法来确定我应该根据响应使用 类 中的哪一个。我目前有这个失败的代码:
var multipleErrors = JsonConvert.DeserializeObject<EnrollDeviceErrorsRoot>(response);
if (multipleErrors.EnrollDeviceErrorResponse != null && multipleErrors.EnrollDeviceErrorResponse.Any())
{
this.StatusCode = multipleErrors.EnrollDeviceErrorResponse.Select(x => x.ErrorCode).Aggregate((a, b) => a + ", " + b);
this.StatusMessage = multipleErrors.EnrollDeviceErrorResponse.Select(x => x.ErrorMessage).Aggregate((a, b) => a + Environment.NewLine + b);
this.HasErrors = true;
return;
}
var singleError = JsonConvert.DeserializeObject<EnrollDeviceErrorRoot>(response);
if (!string.IsNullOrEmpty(singleError.EnrollDeviceErrorResponse.ErrorCode))
{
this.StatusCode = singleError.EnrollDeviceErrorResponse.ErrorCode;
this.StatusMessage = singleError.EnrollDeviceErrorResponse.ErrorMessage;
this.HasErrors = true;
return;
}
以上代码错误:
Newtonsoft.Json.JsonSerializationException : Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Atea.Dep.Core.Service.Models.EnrollDeviceErrorResponse]'
because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer,
not a collection type like an array or List<T>) that can be deserialized from a JSON object.
JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
当其他人不知道他们从服务器返回什么时,他们如何处理来自响应的反序列化?我当然可以只查看原始响应并确定这种方式,但是还有其他更清洁的方法可以做到这一点吗?我无法更改 API,我不知道为什么不能对包含一个或多个错误的列表做出回应。
您可以使用
JObject.Parse(response)["enrollDeviceErrorResponse"].Type
确定响应的类型。在第一种情况下,它将是对象,在第二种情况下 - 数组。然后继续适当的反序列化将很容易。
你可以使用这样的东西:
var output;
dynamic jObject = JsonConvert.DeserializeObject (outputAPI);
bool isArray = jObj.enrollDeviceErrorResponse.Type == JTokenType.Array;
bool isObject = jObj.enrollDeviceErrorResponse.Type == JTokenType.Object;
if(isObject)
output = JsonConvert.DeserializeObject<EnrollDeviceErrorResponse>(outputAPI);
//else you will use the other class to deserialize the object