On any version of .Net that supports the data contract serializers, you can take advantage of the fact that DataContractJsonSerializer inherits from XmlObjectSerializer to call JsonReaderWriterFactory.CreateJsonReader() to create an XmlReader that actually reads JSON, then skip forward to the first nested "element":protected T DeserializeNestedJsonStringWithReader<T>(string jsonString){ var reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.Unicode.GetBytes(jsonString), System.Xml.XmlDictionaryReaderQuotas.Max); int elementCount = 0; while (reader.Read()) { if (reader.NodeType == System.Xml.XmlNodeType.Element) elementCount++; if (elementCount == 2) // At elementCount == 1 there is a synthetic "root" element { var serializer = new DataContractJsonSerializer(typeof(T)); return (T)serializer.ReadObject(reader, false); } } return default(T);}这种技术看起来很奇怪(使用XmlReader解析JSON?),但是通过一些额外的工作,应该可以扩展这种想法以创建SelectToken()的JSON的Simple_API_for_XML"rel =" nofollow>类似于SAX的解析功能,在JSON中向前跳过,直到找到所需的属性,然后反序列化其值.This technique looks odd (parsing JSON with an XmlReader?), but with some extra work it should be possible to extend this idea to create SAX-like parsing functionality for JSON that is similar to SelectToken(), skipping forward in the JSON until a desired property is found, then deserializing its value.例如,要选择和反序列化特定的命名属性,而不仅仅是第一个根属性,可以使用以下内容:For instance, to select and deserialize specific named properties, rather than just the first root property, the following can be used:public static class DataContractJsonSerializerExtensions{ public static T DeserializeNestedJsonProperty<T>(string jsonString, string rootPropertyName) { // Check for count == 2 because there is a synthetic <root> element at the top. Predicate<Stack<string>> match = s => s.Count == 2 && s.Peek() == rootPropertyName; return DeserializeNestedJsonProperties<T>(jsonString, match).FirstOrDefault(); } public static IEnumerable<T> DeserializeNestedJsonProperties<T>(string jsonString, Predicate<Stack<string>> match) { DataContractJsonSerializer serializer = null; using (var reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(jsonString), XmlDictionaryReaderQuotas.Max)) { var stack = new Stack<string>(); while (reader.Read()) { if (reader.NodeType == System.Xml.XmlNodeType.Element) { stack.Push(reader.Name); if (match(stack)) { serializer = serializer ?? new DataContractJsonSerializer(typeof(T)); yield return (T)serializer.ReadObject(reader, false); } if (reader.IsEmptyElement) stack.Pop(); } else if (reader.NodeType == XmlNodeType.EndElement) { stack.Pop(); } } } }}请参见在JSON和XML之间进行映射,以详细了解 JsonReaderWriterFactory 将JSON映射到XML. /p>See Mapping Between JSON and XML for details on how JsonReaderWriterFactory maps JSON to XML. 这篇关于.NET中的JObject.SelectToken等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..