简体   繁体   中英

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!

Never mind, Just did it with:

        JavaScriptSerializer ser = new JavaScriptSerializer();
        List<AccessToken> result = ser.Deserialize<List<AccessToken>>(jsonString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM