Unable to cast object of type 'RestSharp.JsonArray'

2,291 views
Skip to first unread message

Jonathan Channon

unread,
Aug 15, 2012, 12:36:16 PM8/15/12
to rest...@googlegroups.com
This is probably obviously but I can't see the problem.

Below is my code and JSON. Can anyone point me in the right direction?

The response from RestSharp is throwing the error:

    Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.

Is it because the Tags on the first JSON data is null?

**Code**

    var client = new RestClient(this.Context.Request.Url.ToString());
    
    var request = new RestRequest("SugarTown/Posts", Method.GET);
    
    request.AddHeader("Accept", "application/json");
    
    var response2 = client.Execute<List<Post>>(request);


**JSON**

    [
        {
            "Id": "posts/1",
            "Title": "testes jkl",
            "DateCreated": "/Date(-59011459200000)/",
            "Body": "<p><strong>sccsdc</strong></p>",
            "Tags": null
        },
        {
            "Id": "posts/33",
            "Title": "yeah baby",
            "DateCreated": "/Date(-59011459200000)/",
            "Body": "<p><span style=\"text-decoration: underline;\"><strong>jkljk jklj lkjkl kj&nbsp;</strong></span></p>\r\n<ol>\r\n<li><strong><span style=\"text-decoration: underline;\">,lk</span></strong></li>\r\n<li><strong><span style=\"text-decoration: underline;\">98</span></strong></li>\r\n</ol>",
            "Tags": [
                "asd",
                "qwe",
                "fgh",
                "jh"
            ]
        }
    ]

**Model**

    public class Post
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public DateTime DateCreated { get; set; }
        public string Body { get; set; }
        public Collection<string> Tags { get; set; }
    }

Andrew Young

unread,
Aug 15, 2012, 1:01:01 PM8/15/12
to rest...@googlegroups.com
Its probably how you defined Collection. It'll probably work if you make it a List<string>.

Jonathan Channon

unread,
Aug 15, 2012, 2:48:31 PM8/15/12
to rest...@googlegroups.com
I'll give it a try but I've read its not good to expose List<T>

Jonathan Channon

unread,
Aug 15, 2012, 3:35:51 PM8/15/12
to rest...@googlegroups.com
To confirm List<T> resolves the issue
Reply all
Reply to author
Forward
0 new messages