简体   繁体   English

将未命名的json数组反序列化为c#中的对象

[英]Deserialize unnamed json array into an object in c#

Wondering how to deserialize the following string in c#: 想知道如何在c#中反序列化以下字符串:

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

I know how to do it if the json was: 如果json是:我知道怎么做

"{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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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