How do i deserialize this?

641 views
Skip to first unread message

Michiel

unread,
Dec 6, 2012, 3:51:43 AM12/6/12
to rest...@googlegroups.com
I get this string back from my client's server:
{"events": [["specialEvent", "sale_12913"]], "clientID": "serv3:azoth829hgao"}

Getting the clientID is no problem, but the events return a JsonArray which i want in a class aswell:

    public class OModel {
        public string clientID { get; set; }
        public events events { get; set; }
    }

    public class events{
        public string specialEvent{ get; set; }
        public string normalEvent{ get; set; }
    }
 

This however, returns a null and the following exception:
"Unable to cast object of type 'RestSharp.JsonArray' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'."

How do i solve this? 

Nic Wise

unread,
Dec 6, 2012, 4:00:04 AM12/6/12
to rest...@googlegroups.com
I think you have events wrong. It's an array, of array. So...

public string[][] events { get; set; }

possibly? specialEvent is a value in there, not a property name (no : after it)
--
Nic Wise
t. +44 7788 592 806 | @fastchicken | http://www.linkedin.com/in/nicwise
b. http://www.fastchicken.co.nz/

mobileAgent (for FreeAgent): get your accounts in your pocket.
http://goo.gl/IuBU
Trip Wallet: Keep track of your budget on the go: http://goo.gl/ePhKa
Earnest: Self-employed? Track your business expenses and income.
http://earnestapp.com
Nearest Bus: find when the next bus is coming to your stop. http://goo.gl/Vcz1p
London Bike App: Find the nearest Boris Bike, and get riding! http://goo.gl/Icp2

Michiel

unread,
Dec 6, 2012, 4:07:45 AM12/6/12
to rest...@googlegroups.com
Thanks for your quick reply! When i use events as you suggested, the following exception occurs:

No parameterless constructor defined for this object."

With stacktrace

   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at RestSharp.Deserializers.JsonDeserializer.CreateAndMap(Type type, Object element)
   at RestSharp.Deserializers.JsonDeserializer.ConvertValue(Type type, Object value)
   at RestSharp.Deserializers.JsonDeserializer.Map(Object target, IDictionary`2 data)
   at RestSharp.Deserializers.JsonDeserializer.Deserialize[T](IRestResponse response)
   at RestSharp.RestClient.Deserialize[T](IRestRequest request, IRestResponse raw)


Op donderdag 6 december 2012 10:00:04 UTC+1 schreef Nic Wise het volgende:

Andrew Young

unread,
Dec 6, 2012, 12:24:12 PM12/6/12
to rest...@googlegroups.com
Can we see what your class looks like after these changes?

Andrew Young

--
Sent with Sparrow

Michiel

unread,
Dec 9, 2012, 4:22:19 AM12/9/12
to rest...@googlegroups.com
Hello Andrew,

I Managed to get the code working:

    public class OModel {
        public string clientID { get; set; }
        public List<string> events { get; set; }
    }

This isn't the prettiest solution, since now i have "specialEvent", "sale_12913" in a list, but it's managable.

Op donderdag 6 december 2012 18:24:12 UTC+1 schreef ayoung het volgende:

Nic Wise

unread,
Dec 10, 2012, 4:35:13 AM12/10/12
to rest...@googlegroups.com
Interesting. I would have thought that would have rendered as

{"events": ["specialEvent", "sale_12913"], "clientID": "serv3:azoth829hgao"}

not

{"events": [["specialEvent", "sale_12913"]], "clientID": "serv3:azoth829hgao"}

(note the extra [])

Oh, well. Glad it works.
Reply all
Reply to author
Forward
0 new messages