如何使用newtonsoft.json嵌套



我试图用Newtonsoft.JSON包解析JSON。

string jsonData =
 "{"name":"olga","tokenmode":"bearer","expires":9483,"refresh":"{\"Id\":\"alla-ieih-8493j-2455d\",\"Id\":\"94094-3838485-kdooj4u\"}","status":"10","namestatus":"5"}";

 dynamic aa = JsonConvert.DeserializeObject(jsonData);
var name = aa.name.tostring(); //Outputs olga
var id = aa.refresh.Id.ToString(); //Gives error

问题是我如何访问 Id 数据?

您必须添加一个

dynamic refresh = JsonConvert.DeserializeObject(aa.refresh.ToString());

创建一个类:

class Player
{
    public Guid ID { get; set; }
    public string Name { get; set; }
    public TimeSpan Elapsed { get; set; }
    [JsonConstructor]
    public Player(Guid id, string name, TimeSpan elapsed)
    {
        this.ID = id;
        this.Name = name;
        this.Elapsed = elapsed;
    }
    public Player(string name, TimeSpan elapsed)
    {
        this.ID = Guid.NewGuid();
        this.Name = name;
        this.Elapsed = elapsed;
    }
}

询问对象:

Player player = JsonConvert.DeserializeObject<Player>(jsonstring);

询问对象列表:

List<Player> players = JsonConvert.DeserializeObject<List<Player>>(jsonstring);

你必须这样做:

var id = aa.refresh[0].Id.ToString();

相关内容

  • 没有找到相关文章

最新更新