Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize unnamed json array into an object in c#

Wondering how to deserialize the following string in c#:

"[{\"access_token\":\"thisistheaccesstoken\"}]"

I know how to do it if the json was:

"{array=[{\"access_token\":\"thisistheaccesstoken\"}]}"

I'd do it like this:

public class AccessToken
{
    public string access_token {get;set;}
    public DateTime expires { get; set; }
}

public class TokenReturn
{
    public List<AccessToken> tokens { get; set; }
}

JavaScriptSerializer ser = new JavaScriptSerializer();
TokenReturn result = ser.Deserialize<TokenReturn>(responseFromServer);

But without that array name, I'm not sure. Any suggestions?

Thanks!

like image 980
rksprst Avatar asked Oct 14 '22 05:10

rksprst


1 Answers

Never mind, Just did it with:

        JavaScriptSerializer ser = new JavaScriptSerializer();
        List<AccessToken> result = ser.Deserialize<List<AccessToken>>(jsonString);
like image 193
rksprst Avatar answered Oct 18 '22 04:10

rksprst