Parse JSON en C #

Estoy tratando de analizar algunos datos JSON de la API de búsqueda de Google AJAX. Tengo esta URL y me gustaría desglosarla para que se muestren los resultados. Actualmente he escrito este código, pero estoy bastante perdido en cuanto a qué hacer a continuación, aunque hay una serie de ejemplos con cadenas simplificadas de JSON.

Siendo nuevo en C # y .NET en general, he tenido problemas para obtener un resultado de texto genuino para mi página ASP.NET, así que me recomendaron darle una oportunidad a JSON.NET. ¿Podría alguien señalarme en la dirección correcta simplemente escribiendo algún código que contendrá JSON desde la API de búsqueda de Google AJAX e imprimiéndolo en la pantalla?


EDITAR: ¡ TODO FIJO! Todos los resultados funcionan bien. Gracias de nuevo Dreas Grech!

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.ServiceModel.Web; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.IO; using System.Text; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { GoogleSearchResults g1 = new GoogleSearchResults(); const string json = @"{""responseData"": {""results"":[{""GsearchResultClass"":""GwebSearch"",""unescapedUrl"":""http://www.cheese.com/"",""url"":""http://www.cheese.com/"",""visibleUrl"":""www.cheese.com"",""cacheUrl"":""http://www.google.com/search?q\u003dcache:bkg1gwNt8u4J:www.cheese.com"",""title"":""\u003cb\u003eCHEESE\u003c/b\u003e.COM - All about \u003cb\u003echeese\u003c/b\u003e!."",""titleNoFormatting"":""CHEESE.COM - All about cheese!."",""content"":""\u003cb\u003eCheese\u003c/b\u003e - everything you want to know about it. Search \u003cb\u003echeese\u003c/b\u003e by name, by types of milk, by textures and by countries.""},{""GsearchResultClass"":""GwebSearch"",""unescapedUrl"":""http://en.wikipedia.org/wiki/Cheese"",""url"":""http://en.wikipedia.org/wiki/Cheese"",""visibleUrl"":""en.wikipedia.org"",""cacheUrl"":""http://www.google.com/search?q\u003dcache:n9icdgMlCXIJ:en.wikipedia.org"",""title"":""\u003cb\u003eCheese\u003c/b\u003e - Wikipedia, the free encyclopedia"",""titleNoFormatting"":""Cheese - Wikipedia, the free encyclopedia"",""content"":""\u003cb\u003eCheese\u003c/b\u003e is a food consisting of proteins and fat from milk, usually the milk of cows, buffalo, goats, or sheep. It is produced by coagulation of the milk \u003cb\u003e...\u003c/b\u003e""},{""GsearchResultClass"":""GwebSearch"",""unescapedUrl"":""http://www.ilovecheese.com/"",""url"":""http://www.ilovecheese.com/"",""visibleUrl"":""www.ilovecheese.com"",""cacheUrl"":""http://www.google.com/search?q\u003dcache:GBhRR8ytMhQJ:www.ilovecheese.com"",""title"":""I Love \u003cb\u003eCheese\u003c/b\u003e!, Homepage"",""titleNoFormatting"":""I Love Cheese!, Homepage"",""content"":""The American Dairy Association\u0026#39;s official site includes recipes and information on nutrition and storage of \u003cb\u003echeese\u003c/b\u003e.""},{""GsearchResultClass"":""GwebSearch"",""unescapedUrl"":""http://www.gnome.org/projects/cheese/"",""url"":""http://www.gnome.org/projects/cheese/"",""visibleUrl"":""www.gnome.org"",""cacheUrl"":""http://www.google.com/search?q\u003dcache:jvfWnVcSFeQJ:www.gnome.org"",""title"":""\u003cb\u003eCheese\u003c/b\u003e"",""titleNoFormatting"":""Cheese"",""content"":""\u003cb\u003eCheese\u003c/b\u003e uses your webcam to take photos and videos, applies fancy special effects and lets you share the fun with others. It was written as part of Google\u0026#39;s \u003cb\u003e...\u003c/b\u003e""}],""cursor"":{""pages"":[{""start"":""0"",""label"":1},{""start"":""4"",""label"":2},{""start"":""8"",""label"":3},{""start"":""12"",""label"":4},{""start"":""16"",""label"":5},{""start"":""20"",""label"":6},{""start"":""24"",""label"":7},{""start"":""28"",""label"":8}],""estimatedResultCount"":""14400000"",""currentPageIndex"":0,""moreResultsUrl"":""http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den-GB\u0026q\u003dcheese""}}, ""responseDetails"": null, ""responseStatus"": 200}"; g1 = JSONHelper.Deserialise(json); Response.Write(g1.content); } } public class JSONHelper { public static T Deserialise(string json) { T obj = Activator.CreateInstance(); MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)); DataContractJsonSerializer serialiser = new DataContractJsonSerializer(obj.GetType()); ms.Close(); return obj; } } /// Deserialise from JSON [Serializable] public class GoogleSearchResults { public GoogleSearchResults() { } public GoogleSearchResults(string _unescapedUrl, string _url, string _visibleUrl, string _cacheUrl, string _title, string _titleNoFormatting, string _content) { this.unescapedUrl = _unescapedUrl; this.url = _url; this.visibleUrl = _visibleUrl; this.cacheUrl = _cacheUrl; this.title = _title; this.titleNoFormatting = _titleNoFormatting; this.content = _content; } string _unescapedUrl; string _url; string _visibleUrl; string _cacheUrl; string _title; string _titleNoFormatting; string _content; [DataMember] public string unescapedUrl { get { return _unescapedUrl; } set { _unescapedUrl = value; } } [DataMember] public string url { get { return _url; } set { _url = value; } } [DataMember] public string visibleUrl { get { return _visibleUrl; } set { _visibleUrl = value; } } [DataMember] public string cacheUrl { get { return _cacheUrl; } set { _cacheUrl = value; } } [DataMember] public string title { get { return _title; } set { _title = value; } } [DataMember] public string titleNoFormatting { get { return _titleNoFormatting; } set { _titleNoFormatting = value; } } [DataMember] public string content { get { return _content; } set { _content = value; } } } 

El código actualmente se comstack y se ejecuta perfectamente, pero no devuelve ningún resultado. ¿Podría alguien ayudarme a devolver lo que necesito, los resultados listos para imprimir en la pantalla?

Editar:

Json.NET funciona usando el mismo JSON y clases que el ejemplo anterior.

 GoogleSearchResults g1 = JsonConvert.DeserializeObject(json); 

Enlace: Serialización y deserialización de JSON con Json.NET

Relacionado

C #: análisis de datos formateados json en hashtables nesteds
Parse JSON array

[Actualizar]
Me acabo de dar cuenta de por qué no Deserialize resultados … tienes una línea faltante en tu método Deserialize . Olvidaste asignar los resultados a tu obj :

 public static T Deserialize(string json) { using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); return (T)serializer.ReadObject(ms); } } 

Además, solo como referencia, aquí está el método de Serialize :

 public static string Serialize(T obj) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); using (MemoryStream ms = new MemoryStream()) { serializer.WriteObject(ms, obj); return Encoding.Default.GetString(ms.ToArray()); } } 

Editar

Si desea usar Json.NET aquí están los métodos de serialización / deserialización equivalentes al código anterior.

Deserializar:

 JsonConvert.DeserializeObject(string json); 

Publicar por fascículos:

 JsonConvert.SerializeObject(object o); 

Esto ya es parte de Json.NET, así que puedes llamarlos en la clase JsonConvert.

Enlace: Serialización y deserialización de JSON con Json.NET


Ahora, la razón por la que obtienes StackOverflow es por tus Properties .

Tomemos por ejemplo este:

 [DataMember] public string unescapedUrl { get { return unescapedUrl; } // <= this line is causing a Stack Overflow set { this.unescapedUrl = value; } } 

Observe que en el getter , está devolviendo la propiedad real (es decir, el getter de la propiedad se está llamando a sí mismo una y otra vez), y por lo tanto está creando una recursión infinita.


Las propiedades (en 2.0) deberían definirse así:

 string _unescapedUrl; // <= private field [DataMember] public string unescapedUrl { get { return _unescapedUrl; } set { _unescapedUrl = value; } } 

Tiene un campo privado y luego devuelve el valor de ese campo en el getter, y establece el valor de ese campo en el setter.


Por cierto, si está utilizando el Framework 3.5, puede hacer esto y evitar los campos de respaldo, y deje que el comstackdor se encargue de eso:

 public string unescapedUrl { get; set;} 

Su clase de datos no coincide con el objeto JSON. Use esto en su lugar:

 [DataContract] public class GoogleSearchResults { [DataMember] public ResponseData responseData { get; set; } } [DataContract] public class ResponseData { [DataMember] public IEnumerable results { get; set; } } [DataContract] public class Results { [DataMember] public string unescapedUrl { get; set; } [DataMember] public string url { get; set; } [DataMember] public string visibleUrl { get; set; } [DataMember] public string cacheUrl { get; set; } [DataMember] public string title { get; set; } [DataMember] public string titleNoFormatting { get; set; } [DataMember] public string content { get; set; } } 

Además, no es necesario crear una instancia de la clase para obtener su tipo para la deserialización:

 public static T Deserialise(string json) { using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json))) { var serialiser = new DataContractJsonSerializer(typeof(T)); return (T)serialiser.ReadObject(ms); } } 

Encontré este enfoque que analiza JSON en un objeto dynamic , extiende un DynamicObject y JavascriptConverter para convertir la cadena en un objeto.

DynamicJsonObject

 public class DynamicJsonObject : DynamicObject { private IDictionary Dictionary { get; set; } public DynamicJsonObject(IDictionary dictionary) { this.Dictionary = dictionary; } public override bool TryGetMember(GetMemberBinder binder, out object result) { result = this.Dictionary[binder.Name]; if (result is IDictionary) { result = new DynamicJsonObject(result as IDictionary); } else if (result is ArrayList && (result as ArrayList) is IDictionary) { result = new List((result as ArrayList).ToArray().Select(x => new DynamicJsonObject(x as IDictionary))); } else if (result is ArrayList) { result = new List((result as ArrayList).ToArray()); } return this.Dictionary.ContainsKey(binder.Name); } } 

Convertidor

 public class DynamicJsonConverter : JavaScriptConverter { public override object Deserialize(IDictionary dictionary, Type type, JavaScriptSerializer serializer) { if (dictionary == null) throw new ArgumentNullException("dictionary"); if (type == typeof(object)) { return new DynamicJsonObject(dictionary); } return null; } public override IDictionary Serialize(object obj, JavaScriptSerializer serializer) { throw new NotImplementedException(); } public override IEnumerable SupportedTypes { get { return new ReadOnlyCollection(new List(new Type[] { typeof(object) })); } } } 

Uso ( muestra json ):

 JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() }); dynamic glossaryEntry = jss.Deserialize(json, typeof(object)) as dynamic; Console.WriteLine("glossaryEntry.glossary.title: " + glossaryEntry.glossary.title); Console.WriteLine("glossaryEntry.glossary.GlossDiv.title: " + glossaryEntry.glossary.GlossDiv.title); Console.WriteLine("glossaryEntry.glossary.GlossDiv.GlossList.GlossEntry.ID: " + glossaryEntry.glossary.GlossDiv.GlossList.GlossEntry.ID); Console.WriteLine("glossaryEntry.glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para: " + glossaryEntry.glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para); foreach (var also in glossaryEntry.glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso) { Console.WriteLine("glossaryEntry.glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso: " + also); } 

Este método debe ser verdadero, de lo contrario generará un error. Por ejemplo, puede lanzar un error si una clave no existe.

Devolver el resultado true y vaciar devolverá un valor vacío en lugar de arrojar un error.

 public override bool TryGetMember(GetMemberBinder binder, out object result) { if (!this.Dictionary.ContainsKey(binder.Name)) { result = ""; } else { result = this.Dictionary[binder.Name]; } if (result is IDictionary) { result = new DynamicJsonObject(result as IDictionary); } else if (result is ArrayList && (result as ArrayList) is IDictionary) { result = new List((result as ArrayList).ToArray().Select(x => new DynamicJsonObject(x as IDictionary))); } else if (result is ArrayList) { result = new List((result as ArrayList).ToArray()); } return true; // this.Dictionary.ContainsKey(binder.Name); } 

Solo creo que todo el ejemplo sería útil. Este es el ejemplo de este problema.

 using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.ServiceModel.Web; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.IO; using System.Text; using System.Collections.Generic; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { GoogleSearchResults g1 = new GoogleSearchResults(); const string json = @"{""responseData"": {""results"":[{""GsearchResultClass"":""GwebSearch"",""unescapedUrl"":""http://www.cheese.com/"",""url"":""http://www.cheese.com/"",""visibleUrl"":""www.cheese.com"",""cacheUrl"":""http://www.google.com/search?q\u003dcache:bkg1gwNt8u4J:www.cheese.com"",""title"":""\u003cb\u003eCHEESE\u003c/b\u003e.COM - All about \u003cb\u003echeese\u003c/b\u003e!."",""titleNoFormatting"":""CHEESE.COM - All about cheese!."",""content"":""\u003cb\u003eCheese\u003c/b\u003e - everything you want to know about it. Search \u003cb\u003echeese\u003c/b\u003e by name, by types of milk, by textures and by countries.""},{""GsearchResultClass"":""GwebSearch"",""unescapedUrl"":""http://en.wikipedia.org/wiki/Cheese"",""url"":""http://en.wikipedia.org/wiki/Cheese"",""visibleUrl"":""en.wikipedia.org"",""cacheUrl"":""http://www.google.com/search?q\u003dcache:n9icdgMlCXIJ:en.wikipedia.org"",""title"":""\u003cb\u003eCheese\u003c/b\u003e - Wikipedia, the free encyclopedia"",""titleNoFormatting"":""Cheese - Wikipedia, the free encyclopedia"",""content"":""\u003cb\u003eCheese\u003c/b\u003e is a food consisting of proteins and fat from milk, usually the milk of cows, buffalo, goats, or sheep. It is produced by coagulation of the milk \u003cb\u003e...\u003c/b\u003e""},{""GsearchResultClass"":""GwebSearch"",""unescapedUrl"":""http://www.ilovecheese.com/"",""url"":""http://www.ilovecheese.com/"",""visibleUrl"":""www.ilovecheese.com"",""cacheUrl"":""http://www.google.com/search?q\u003dcache:GBhRR8ytMhQJ:www.ilovecheese.com"",""title"":""I Love \u003cb\u003eCheese\u003c/b\u003e!, Homepage"",""titleNoFormatting"":""I Love Cheese!, Homepage"",""content"":""The American Dairy Association\u0026#39;s official site includes recipes and information on nutrition and storage of \u003cb\u003echeese\u003c/b\u003e.""},{""GsearchResultClass"":""GwebSearch"",""unescapedUrl"":""http://www.gnome.org/projects/cheese/"",""url"":""http://www.gnome.org/projects/cheese/"",""visibleUrl"":""www.gnome.org"",""cacheUrl"":""http://www.google.com/search?q\u003dcache:jvfWnVcSFeQJ:www.gnome.org"",""title"":""\u003cb\u003eCheese\u003c/b\u003e"",""titleNoFormatting"":""Cheese"",""content"":""\u003cb\u003eCheese\u003c/b\u003e uses your webcam to take photos and videos, applies fancy special effects and lets you share the fun with others. It was written as part of Google\u0026#39;s \u003cb\u003e...\u003c/b\u003e""}],""cursor"":{""pages"":[{""start"":""0"",""label"":1},{""start"":""4"",""label"":2},{""start"":""8"",""label"":3},{""start"":""12"",""label"":4},{""start"":""16"",""label"":5},{""start"":""20"",""label"":6},{""start"":""24"",""label"":7},{""start"":""28"",""label"":8}],""estimatedResultCount"":""14400000"",""currentPageIndex"":0,""moreResultsUrl"":""http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den-GB\u0026q\u003dcheese""}}, ""responseDetails"": null, ""responseStatus"": 200}"; g1 = JSONHelper.Deserialise(json); foreach (Pages x in g1.responseData.cursor.pages) { // Anything you want to get Response.Write(x.label); } } } public class JSONHelper { public static T Deserialise(string json) { using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json))) { var serialiser = new DataContractJsonSerializer(typeof(T)); return (T)serialiser.ReadObject(ms); } } public static string Serialize(T obj) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); using (MemoryStream ms = new MemoryStream()) { serializer.WriteObject(ms, obj); return Encoding.Default.GetString(ms.ToArray()); } } } [DataContract] public class GoogleSearchResults { [DataMember] public ResponseData responseData { get; set; } [DataMember] public string responseStatus { get; set; } } public class ResponseData { [DataMember] public Cursor cursor { get; set; } [DataMember] public IEnumerable results { get; set; } } [DataContract] public class Cursor { [DataMember] public IEnumerable pages { get; set; } } [DataContract] public class Pages { [DataMember] public string start { get; set; } [DataMember] public string label { get; set; } } [DataContract] public class Results { [DataMember] public string unescapedUrl { get; set; } [DataMember] public string url { get; set; } [DataMember] public string visibleUrl { get; set; } [DataMember] public string cacheUrl { get; set; } [DataMember] public string title { get; set; } [DataMember] public string titleNoFormatting { get; set; } [DataMember] public string content { get; set; } } 

Intenté usar el código anterior pero no funcionó. La estructura JSON devuelta por Google es muy diferente y hay una falla muy importante en la función auxiliar: una llamada a DataContractJsonSerializer.ReadObject() que realmente deserializa los datos JSON en el objeto.

Aquí está el código que FUNCIONA en 2011:

 using System; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.IO; using System.Text; using System.Collections.Generic; namespace  { public class JSONHelper { public static T Deserialise(string json) { T obj = Activator.CreateInstance(); MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)); DataContractJsonSerializer serialiser = new DataContractJsonSerializer(obj.GetType()); obj = (T)serialiser.ReadObject(ms); ms.Close(); return obj; } } public class Result { public string GsearchResultClass { get; set; } public string unescapedUrl { get; set; } public string url { get; set; } public string visibleUrl { get; set; } public string cacheUrl { get; set; } public string title { get; set; } public string titleNoFormatting { get; set; } public string content { get; set; } } public class Page { public string start { get; set; } public int label { get; set; } } public class Cursor { public string resultCount { get; set; } public Page[] pages { get; set; } public string estimatedResultCount { get; set; } public int currentPageIndex { get; set; } public string moreResultsUrl { get; set; } public string searchResultTime { get; set; } } public class ResponseData { public Result[] results { get; set; } public Cursor cursor { get; set; } } public class GoogleSearchResults { public ResponseData responseData { get; set; } public object responseDetails { get; set; } public int responseStatus { get; set; } } } 

Para obtener el contenido del primer resultado, hazlo:

 GoogleSearchResults googleResults = new GoogleSearchResults(); googleResults = JSONHelper.Deserialise(jsonData); string contentOfFirstResult = googleResults.responseData.results[0].content; 

Gracias por toda tu ayuda. Esta es mi versión final, ¡y funciona gracias a tu ayuda combinada! Solo estoy mostrando los cambios que hice, todo lo demás está tomado del trabajo de Joe Chung

 public class GoogleSearchResults { [DataMember] public ResponseData responseData { get; set; } [DataMember] public string responseDetails { get; set; } [DataMember] public int responseStatus { get; set; } } 

y

  [DataContract] public class ResponseData { [DataMember] public List results { get; set; } } 

Solicitud de Google Map API y analizar DirectionsResponse con C #, cambie el json en su url a xml y use el siguiente código para convertir el resultado en un objeto de lista genérico C # utilizable.

Me tomó un tiempo hacer. Pero aquí está

 var url = String.Format("http://maps.googleapis.com/maps/api/directions/xml?..."); var result = new System.Net.WebClient().DownloadString(url); var doc = XDocument.Load(new StringReader(result)); var DirectionsResponse = doc.Elements("DirectionsResponse").Select(l => new { Status = l.Elements("status").Select(q => q.Value).FirstOrDefault(), Route = l.Descendants("route").Select(n => new { Summary = n.Elements("summary").Select(q => q.Value).FirstOrDefault(), Leg = n.Elements("leg").ToList().Select(o => new { Step = o.Elements("step").Select(p => new { Travel_Mode = p.Elements("travel_mode").Select(q => q.Value).FirstOrDefault(), Start_Location = p.Elements("start_location").Select(q => new { Lat = q.Elements("lat").Select(r => r.Value).FirstOrDefault(), Lng = q.Elements("lng").Select(r => r.Value).FirstOrDefault() }).FirstOrDefault(), End_Location = p.Elements("end_location").Select(q => new { Lat = q.Elements("lat").Select(r => r.Value).FirstOrDefault(), Lng = q.Elements("lng").Select(r => r.Value).FirstOrDefault() }).FirstOrDefault(), Polyline = p.Elements("polyline").Select(q => new { Points = q.Elements("points").Select(r => r.Value).FirstOrDefault() }).FirstOrDefault(), Duration = p.Elements("duration").Select(q => new { Value = q.Elements("value").Select(r => r.Value).FirstOrDefault(), Text = q.Elements("text").Select(r => r.Value).FirstOrDefault(), }).FirstOrDefault(), Html_Instructions = p.Elements("html_instructions").Select(q => q.Value).FirstOrDefault(), Distance = p.Elements("distance").Select(q => new { Value = q.Elements("value").Select(r => r.Value).FirstOrDefault(), Text = q.Elements("text").Select(r => r.Value).FirstOrDefault(), }).FirstOrDefault() }).ToList(), Duration = o.Elements("duration").Select(p => new { Value = p.Elements("value").Select(q => q.Value).FirstOrDefault(), Text = p.Elements("text").Select(q => q.Value).FirstOrDefault() }).FirstOrDefault(), Distance = o.Elements("distance").Select(p => new { Value = p.Elements("value").Select(q => q.Value).FirstOrDefault(), Text = p.Elements("text").Select(q => q.Value).FirstOrDefault() }).FirstOrDefault(), Start_Location = o.Elements("start_location").Select(p => new { Lat = p.Elements("lat").Select(q => q.Value).FirstOrDefault(), Lng = p.Elements("lng").Select(q => q.Value).FirstOrDefault() }).FirstOrDefault(), End_Location = o.Elements("end_location").Select(p => new { Lat = p.Elements("lat").Select(q => q.Value).FirstOrDefault(), Lng = p.Elements("lng").Select(q => q.Value).FirstOrDefault() }).FirstOrDefault(), Start_Address = o.Elements("start_address").Select(q => q.Value).FirstOrDefault(), End_Address = o.Elements("end_address").Select(q => q.Value).FirstOrDefault() }).ToList(), Copyrights = n.Elements("copyrights").Select(q => q.Value).FirstOrDefault(), Overview_polyline = n.Elements("overview_polyline").Select(q => new { Points = q.Elements("points").Select(r => r.Value).FirstOrDefault() }).FirstOrDefault(), Waypoint_Index = n.Elements("waypoint_index").Select(o => o.Value).ToList(), Bounds = n.Elements("bounds").Select(q => new { SouthWest = q.Elements("southwest").Select(r => new { Lat = r.Elements("lat").Select(s => s.Value).FirstOrDefault(), Lng = r.Elements("lng").Select(s => s.Value).FirstOrDefault() }).FirstOrDefault(), NorthEast = q.Elements("northeast").Select(r => new { Lat = r.Elements("lat").Select(s => s.Value).FirstOrDefault(), Lng = r.Elements("lng").Select(s => s.Value).FirstOrDefault() }).FirstOrDefault(), }).FirstOrDefault() }).FirstOrDefault() }).FirstOrDefault(); 

Espero que esto ayude a alguien.