Here are the classes that get generated when using the past as JSON classes as suggested by snow_FFFFFF:public class Rootobject{ public int timestamp { get; set; } public int total_players { get; set; } public int max_score { get; set; } public Players players { get; set; }}public class Players{ public object[] Player1Username { get; set; } public object[] Player2Username { get; set; }}我不清楚我如何将"players"元素中的JSON数据反序列化为List,而Player1Username是Player对象上的简单字符串属性.至于混合字符串和整数的集合,我相信我可以毫无问题地将它们放入Player对象的各个属性中.What is not clear to me is how do I deserialize the JSON data in the "players" element as a List with Player1Username being a simple string property on the Player object. As for the collection of intermixed strings and integers, I am confident I can get those into individual properties on the Player object without issue.推荐答案 中的转换器在Visual Basic .NET中反序列化JSON 应该可以满足您的需要,可以适当地将其从VB.NET转换为c#:The converter from Deserializing JSON in Visual Basic .NET should do what you need, suitably translated from VB.NET to c#:public class ObjectToArrayConverter<T> : JsonConverter{ public override bool CanConvert(Type objectType) { return typeof(T) == objectType; } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var objectType = value.GetType(); var contract = serializer.ContractResolver.ResolveContract(objectType) as JsonObjectContract; if (contract == null) throw new JsonSerializationException(string.Format("invalid type {0}.", objectType.FullName)); writer.WriteStartArray(); foreach (var property in SerializableProperties(contract)) { var propertyValue = property.ValueProvider.GetValue(value); if (property.Converter != null && property.Converter.CanWrite) property.Converter.WriteJson(writer, propertyValue, serializer); else serializer.Serialize(writer, propertyValue); } writer.WriteEndArray(); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var contract = serializer.ContractResolver.ResolveContract(objectType) as JsonObjectContract; if (contract == null) throw new JsonSerializationException(string.Format("invalid type {0}.", objectType.FullName)); if (reader.MoveToContentAndAssert().TokenType == JsonToken.Null) return null; if (reader.TokenType != JsonToken.StartArray) throw new JsonSerializationException(string.Format("token {0} was not JsonToken.StartArray", reader.TokenType)); // Not implemented: JsonObjectContract.CreatorParameters, serialization callbacks, existingValue = existingValue ?? contract.DefaultCreator(); using (var enumerator = SerializableProperties(contract).GetEnumerator()) { while (true) { switch (reader.ReadToContentAndAssert().TokenType) { case JsonToken.EndArray: return existingValue; default: if (!enumerator.MoveNext()) { reader.Skip(); break; } var property = enumerator.Current; object propertyValue; // TODO: // https://www.newtonsoft.com/json/help/html/Properties_T_Newtonsoft_Json_Serialization_JsonProperty.htm // JsonProperty.ItemConverter, ItemIsReference, ItemReferenceLoopHandling, ItemTypeNameHandling, DefaultValue, DefaultValueHandling, ReferenceLoopHandling, Required, TypeNameHandling, ... if (property.Converter != null && property.Converter.CanRead) propertyValue = property.Converter.ReadJson(reader, property.PropertyType, property.ValueProvider.GetValue(existingValue), serializer); else propertyValue = serializer.Deserialize(reader, property.PropertyType); property.ValueProvider.SetValue(existingValue, propertyValue); break; } } } } static IEnumerable<JsonProperty> SerializableProperties(JsonObjectContract contract) { return contract.Properties.Where(p => !p.Ignored && p.Readable && p.Writable); }}public static partial class JsonExtensions{ public static JsonReader ReadToContentAndAssert(this JsonReader reader) { return reader.ReadAndAssert().MoveToContentAndAssert(); } public static JsonReader MoveToContentAndAssert(this JsonReader reader) { if (reader == null) throw new ArgumentNullException(); if (reader.TokenType == JsonToken.None) // Skip past beginning of stream. reader.ReadAndAssert(); while (reader.TokenType == JsonToken.Comment) // Skip past comments. reader.ReadAndAssert(); return reader; } public static JsonReader ReadAndAssert(this JsonReader reader) { if (reader == null) throw new ArgumentNullException(); if (!reader.Read()) throw new JsonReaderException("Unexpected end of JSON stream."); return reader; }}接下来,将转换器添加到您的Player类中,并使用 JsonPropertyAttribute.Order :Next, add the converter to your Player class, and indicate the order of each property using JsonPropertyAttribute.Order:[JsonConverter(typeof(ObjectToArrayConverter<Player>))]public class Player{ [JsonProperty(Order = 1)] public int UniqueID { get; set; } [JsonProperty(Order = 2)] public string PlayerDescription { get; set; } // Other fields as required.}然后最后,如下声明您的根对象:Then finally, declare your root object as follows:public class ScoreboardResults{ public int timestamp { get; set; } public int total_players { get; set; } public int max_score { get; set; } public Dictionary<string, Player> players { get; set; }}请注意,我已将Username从Player类移出并移到了字典中,作为键.Note that I have moved Username out of the Player class and into the dictionary, as a key.演示小提琴此处和此处. 这篇关于如何将包含具有固定模式的值数组的对象反序列化为强类型数据类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 00:55