Click here to Skip to main content
15,922,007 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..I have an issue with json serialization which throws the error: 

"Could not cast or convert from System.String to System.Collections.Generic.IList`1[System.String].]".
 
My json: "{\"Type\": \"abc\",\"Filepath\": \"D:\\\\abc.doc\"}"; 
My class: 
public class Arguments { public string Type { get; set; } 
public IList Filepath { get; set; } } 
My code to deserialize: 
var jsonObj = new JavaScriptSerializer().Deserialize(json); 

Can you help me understand the issue? I have made it a list as there can be situation to receive multiple objects and hence it is required. Would be great if you can help me!

It works fine when i have multiple values, ie:
 "{\"Type\": \"abc\",\"Filepath\": [\"D:\\\\abc1.doc\",\"D:\\\\abc2.doc\"]}";
I want it to work fine in both ways.


What I have tried:

My code to deserialize: 
var jsonObj = new JavaScriptSerializer().Deserialize(json); 
Posted
Updated 5-Mar-19 3:24am

That's a terrible JSON schema. You'll have to use JSON.NET[^] with a custom converter to read it.

This StackOverflow thread[^] has an example:
C#
class SingleOrArrayConverter<T> : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(List<T>));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JToken token = JToken.Load(reader);
        if (token.Type == JTokenType.Array)
        {
            return token.ToObject<List<T>>();
        }
        return new List<T> { token.ToObject<T>() };
    }

    public override bool CanWrite
    {
        get { return false; }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        List<T> list = (List<T>)value;
        if (list.Count == 1)
        {
            value = list[0];
        }
        serializer.Serialize(writer, value);
    }
}

...

[JsonConverter(typeof(SingleOrArrayConverter<string>))]
public List<string> Filepath { get; set; }
 
Share this answer
 
v2
Comments
Member 7677029 5-Mar-19 9:27am    
Thanks for the response, however i cannot use json.net :(
Richard Deeming 5-Mar-19 9:29am    
The you'll have to get the person who's generating the JSON to fix their schema so that it always sends an array. The JavaScriptSerializer class can't cope with a property being either an array or a single value.
Graeme_Grant 5-Mar-19 13:14pm    
5'd
Okay, this
{
    "Type": "abc",
    "Filepath": "D:\\abc.doc"
}

deserializes to this:
C#
public class Example
{
    public string Type { get; set; }
    public string Filepath { get; set; }
}

And this
{
    "Type": "abc",
    "Filepath": ["D:\\abc1.doc","D:\\abc2.doc"]
}

deserializes to this:
C#
public class Example
{
    public string Type { get; set; }
    public IList<string> Filepath { get; set; }
}

So the error that you are seeing:
"Could not cast or convert from System.String to System.Collections.Generic.IList`1[System.String].]".

Should now have more meaning. So you can not deserialize this:
{
    "Type": "abc",
    "Filepath": "D:\\abc.doc"
}

to this:
C#
public class Example
{
    public string Type { get; set; }
    public IList<string> Filepath { get; set; }
}

For more information on working with deserializing JSON, check out this article: Working with JSON in C# & VB[^]
 
Share this answer
 
Comments
Richard Deeming 5-Mar-19 9:25am    
You can deserialize it if you use JSON.NET with a custom converter. :)
Graeme_Grant 5-Mar-19 13:13pm    
Yes, you can and I almost did include a converter. But I felt, based on the question that it was both technically the wrong answer and too advanced for the OP. The linked article does cover custom converters in great detail.:)
Member 7677029 5-Mar-19 9:26am    
Thanks for the response!
Graeme_Grant 5-Mar-19 13:15pm    
You are most welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900