本文整理汇总了C#中Newtonsoft.Json.JsonTextReader.MatchNext方法的典型用法代码示例。如果您正苦于以下问题:C# JsonTextReader.MatchNext方法的具体用法?C# JsonTextReader.MatchNext怎么用?C# JsonTextReader.MatchNext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.JsonTextReader
的用法示例。
在下文中一共展示了JsonTextReader.MatchNext方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Deserialize
public static SessionStats Deserialize(string data) {
try {
using (var textReader = new StringReader(data)) {
var reader = new JsonTextReader(textReader);
var r = new SessionStats();
var currentProperty = string.Empty;
reader.MatchNext(JsonToken.StartObject);
while (reader.Until(JsonToken.EndObject)) {
switch (reader.TokenType) {
case JsonToken.PropertyName:
currentProperty = reader.Value.ToString();
break;
case JsonToken.String:
switch (currentProperty) {
case nameof(CarId):
r.CarId = reader.Value.ToString();
break;
case nameof(TrackId):
r.TrackId = reader.Value.ToString();
break;
case nameof(Time):
r.Time = TimeSpan.Parse(reader.Value.ToString());
break;
case nameof(BestLap):
r.BestLap = TimeSpan.Parse(reader.Value.ToString());
break;
default:
throw new Exception($"Unknown key: {currentProperty} (String)");
}
break;
case JsonToken.Date:
switch (currentProperty) {
case nameof(StartedAt):
r.StartedAt = DateTime.Parse(reader.Value.ToString());
break;
default:
throw new Exception($"Unknown key: {currentProperty} (Date)");
}
break;
case JsonToken.Float:
case JsonToken.Integer:
var val = double.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
switch (currentProperty) {
case nameof(MaxSpeed):
r.MaxSpeed = val;
break;
case nameof(Distance):
r.Distance = val;
break;
case nameof(FuelBurnt):
r.FuelBurnt = val;
break;
case nameof(LongestAirborne):
r.LongestAirborne = val;
break;
case nameof(TotalAirborne):
r.TotalAirborne = val;
break;
case nameof(LongestWheelie):
r.LongestWheelie = val;
break;
case nameof(TotalWheelie):
r.TotalWheelie = val;
break;
case nameof(LongestTwoWheels):
r.LongestTwoWheels = val;
break;
case nameof(TotalTwoWheels):
r.TotalTwoWheels = val;
break;
case nameof(TotalTyreWear):
r.TotalTyreWear = val;
break;
case nameof(GoneOffroad):
r.GoneOffroad = (int)val;
break;
case nameof(TotalCrashes):
r.TotalCrashes = (int)val;
break;
case nameof(BestLapId):
r.BestLapId = (int?)val;
break;
default:
throw new Exception($"Unknown key: {currentProperty} (Integer/Float)");
}
break;
case JsonToken.Boolean:
switch (currentProperty) {
case nameof(Penalties):
r.Penalties = bool.Parse(reader.Value.ToString());
break;
default:
throw new Exception($"Unknown key: {currentProperty} (Boolean)");
}
//.........这里部分代码省略.........
示例2: Deserialize
public static ServerInformation[] Deserialize(Stream stream) {
var reader = new JsonTextReader(new StreamReader(stream));
var response = new List<ServerInformation>(1200);
var currentProperty = string.Empty;
reader.MatchNext(JsonToken.StartArray);
while (reader.IsMatchNext(JsonToken.StartObject)) {
var entry = new ServerInformation();
while (reader.Until(JsonToken.EndObject)) {
switch (reader.TokenType) {
case JsonToken.PropertyName:
currentProperty = reader.Value.ToString();
break;
case JsonToken.String:
switch (currentProperty) {
case "ip":
entry.Ip = reader.Value.ToString();
break;
case "name":
entry.Name = reader.Value.ToString();
break;
case "track":
entry.TrackId = reader.Value.ToString();
break;
}
break;
case JsonToken.Integer:
switch (currentProperty) {
case "port":
entry.Port = int.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
break;
case "cport":
entry.PortC = int.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
break;
case "tport":
entry.PortT = int.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
break;
case "clients":
entry.Clients = int.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
break;
case "maxclients":
entry.Capacity = int.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
break;
case "timeofday":
entry.Time = int.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
break;
case "session":
entry.Session = int.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
break;
case "timeleft":
entry.TimeLeft = long.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
break;
case "timestamp":
entry.Timestamp = long.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
break;
case "lastupdate":
entry.LastUpdate = long.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
break;
}
break;
case JsonToken.Boolean:
switch (currentProperty) {
case "pass":
entry.Password = bool.Parse(reader.Value.ToString());
break;
case "pickup":
entry.PickUp = bool.Parse(reader.Value.ToString());
break;
case "l":
entry.L = bool.Parse(reader.Value.ToString());
break;
}
break;
case JsonToken.StartArray:
switch (currentProperty) {
case "cars":
entry.CarIds = reader.ReadStringArray(1);
break;
case "sessiontypes":
entry.SessionTypes = reader.ReadIntArray(1);
break;
case "durations":
entry.Durations = reader.ReadLongArray(1);
break;
case "country":
entry.Country = reader.ReadStringArray(2);
break;
default:
while (reader.Until(JsonToken.EndArray)) { }
break;
}
break;
default:
//.........这里部分代码省略.........