Home:ALL Converter>C# JSON deserialization of array/dictionary

C# JSON deserialization of array/dictionary

Ask Time:2018-02-01T16:42:24         Author:pejoter

Json Formatter

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.

Author:pejoter,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48558497/c-sharp-json-deserialization-of-array-dictionary
D-Shih :

You may use model object like this.\n\nThe public Dictionary<string, string[]> details { get; set; }, Just use dynamic rather than Dictionary<string, string[]>.\n\nBecause the json string detail is unfixed.\n\npublic class Error\n{\n public int error_code { get; set; }\n public string desc { get; set; }\n public dynamic details { get; set; }\n}\n\npublic class ErrorObj\n{\n public string result { get; set; }\n public Error error { get; set; }\n}\n\n\nIf you want to know,which json did you get? \n\nYou can use detail.GetType(). To check the type is or isn't array.\n\nlike this simple\n\nstring DictJson = \"{\\\"result\\\":\\\"FAIL\\\",\\\"error\\\":{\\\"error_code\\\":1,\\\"desc\\\":\\\"INVALID_DATA\\\",\\\"details\\\":{\\\"city\\\":[\\\"City cannot be blank.\\\"]}}}\";\n\nstring ArrayJson = \"{\\\"result\\\":\\\"FAIL\\\",\\\"error\\\":{\\\"error_code\\\":1,\\\"desc\\\":\\\"ERROR_OPTIONS\\\",\\\"details\\\":[\\\"Specifying a bank account\\\"]}}\";\n\nErrorObj errorobj = DeSerializeObjectFromJsonString<ErrorObj>(ArrayJson);\n\nif (errorobj.error.details.GetType().IsArray)\n{\n //receive array detail\n}\nelse\n{\n //receive Dictionary<> detail\n}\n",
2018-02-01T08:55:12
yy