I must consome web service (json). I build a communication with serialization / deserialization with JavaScriptSerializer.
In 99% its works fine, but... Error details is returned like that:
{"result":"FAIL","error":{"error_code":1,"desc":"INVALID_DATA","details":{"city":["City cannot be blank."]}}}
To handle that i created Class:
public class ErrorObj
{
public int error_code { get; set; }
public string desc { get; set; }
public Dictionary<string, string[]> details { get; set; }
}
But somtethimes 'details' is returned like that:
{"result":"FAIL","error":{"error_code":1,"desc":"ERROR_OPTIONS","details":["Specifying a bank account"]}}
or
{"result":"FAIL","error":{"error_code":1,"desc":"INVALID_DATA","details":[]}}
To handle thi the class should be like that:
public class ErrorObj
{
public int error_code { get; set; }
public string desc { get; set; }
public string[] details { get; set; }
}
How to build object (ErrorObj
) to handle all error messages?
Deserialization code:
public static T DeSerializeObjectFromJsonString<T>(string jsonString)
{
T objectOut = default(T);
Type outType = typeof(T);
var obj = (T) new JavaScriptSerializer().Deserialize(jsonString, typeof(T));
return obj;
}
Error message:
System.InvalidOperationException : Type 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089], [System.String[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' is not supported for deserialization of an array.