我试图用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数据?
答案 0 :(得分:4)
您必须添加
dynamic refresh = JsonConvert.DeserializeObject(aa.refresh.ToString());
答案 1 :(得分:1)
创建一个类:
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);
答案 2 :(得分:0)
你必须这样做:
var id = aa.refresh[0].Id.ToString();