反序列化json响应



我使用的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

以确定响应的类型。在第一种情况下,它将是obect,在第二个阵列中。这样就可以很容易地继续进行正确的反序列化。

您可以使用这样的东西:

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

相关内容

  • 没有找到相关文章

最新更新