Parse JSON en C #

J’essaie d’parsingr certaines données JSON à partir de l’API Google AJAX Search. J’ai cette URL et j’aimerais la décomposer pour que les résultats soient affichés. J’ai actuellement écrit ce code, mais je ne sais pas quoi faire par la suite, même s’il existe plusieurs exemples de chaînes JSON simplifiées.

Étant nouveau pour C # et .NET en général, j’ai eu du mal à obtenir une sortie de texte authentique pour ma page ASP.NET. On m’a donc recommandé d’essayer JSON.NET. Quelqu’un pourrait-il me diriger dans la bonne direction en écrivant simplement du code qui prendra en JSON de l’API de recherche Google AJAX et l’imprimer à l’écran?


EDIT: TOUS FIXES! Tous les résultats fonctionnent bien. Merci encore 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 ssortingng 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 counsortinges.""},{""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 nusortingtion 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(ssortingng 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(ssortingng _unescapedUrl, ssortingng _url, ssortingng _visibleUrl, ssortingng _cacheUrl, ssortingng _title, ssortingng _titleNoFormatting, ssortingng _content) { this.unescapedUrl = _unescapedUrl; this.url = _url; this.visibleUrl = _visibleUrl; this.cacheUrl = _cacheUrl; this.title = _title; this.titleNoFormatting = _titleNoFormatting; this.content = _content; } ssortingng _unescapedUrl; ssortingng _url; ssortingng _visibleUrl; ssortingng _cacheUrl; ssortingng _title; ssortingng _titleNoFormatting; ssortingng _content; [DataMember] public ssortingng unescapedUrl { get { return _unescapedUrl; } set { _unescapedUrl = value; } } [DataMember] public ssortingng url { get { return _url; } set { _url = value; } } [DataMember] public ssortingng visibleUrl { get { return _visibleUrl; } set { _visibleUrl = value; } } [DataMember] public ssortingng cacheUrl { get { return _cacheUrl; } set { _cacheUrl = value; } } [DataMember] public ssortingng title { get { return _title; } set { _title = value; } } [DataMember] public ssortingng titleNoFormatting { get { return _titleNoFormatting; } set { _titleNoFormatting = value; } } [DataMember] public ssortingng content { get { return _content; } set { _content = value; } } } 

Le code comstack actuellement et fonctionne parfaitement, mais ne renvoie aucun résultat. Quelqu’un pourrait-il m’aider à retourner ce dont j’ai besoin, les résultats prêts à imprimer à l’écran?

Modifier:

Json.NET fonctionne avec le même JSON et les mêmes classes que l’exemple ci-dessus.

 GoogleSearchResults g1 = JsonConvert.DeserializeObject(json); 

Lien: sérialisation et désérialisation de JSON avec Json.NET

en relation

C # – Analyse des données au format json dans des hashtables nestedes
Parse JSON array

[Mettre à jour]
Je viens juste de comprendre pourquoi vous ne receviez pas de résultats … vous avez une ligne manquante dans votre méthode Deserialize . Vous avez oublié d’atsortingbuer les résultats à votre obj :

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

Aussi, juste pour référence, voici la méthode Serialize :

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

modifier

Si vous voulez utiliser Json.NET, voici les méthodes Serialize / Deserialize équivalentes au code ci-dessus.

Désérialiser:

 JsonConvert.DeserializeObject(ssortingng json); 

Sérialiser:

 JsonConvert.SerializeObject(object o); 

Cela fait déjà partie de Json.NET, vous pouvez donc simplement les appeler sur la classe JsonConvert.

Lien: sérialisation et désérialisation de JSON avec Json.NET


La raison pour laquelle vous obtenez un StackOverflow est due à vos Properties .

Prenons par exemple celui-ci:

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

Notez que dans le getter , vous retournez la propriété réelle (ie le getter de la propriété appelle lui-même à plusieurs resockets), et vous créez ainsi une récursion infinie.


Les propriétés (en 2.0) doivent être définies comme suit:

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

Vous avez un champ privé, puis vous retournez la valeur de ce champ dans le getter et définissez la valeur de ce champ dans le setter.


Btw, si vous utilisez le Framework 3.5, vous pouvez simplement faire cela et éviter les champs de sauvegarde, et laisser le compilateur s'en occuper:

 public ssortingng unescapedUrl { get; set;} 

Votre classe de données ne correspond pas à l’object JSON. Utilisez ceci à la place:

 [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 ssortingng unescapedUrl { get; set; } [DataMember] public ssortingng url { get; set; } [DataMember] public ssortingng visibleUrl { get; set; } [DataMember] public ssortingng cacheUrl { get; set; } [DataMember] public ssortingng title { get; set; } [DataMember] public ssortingng titleNoFormatting { get; set; } [DataMember] public ssortingng content { get; set; } } 

De plus, vous n’avez pas besoin d’instancier la classe pour obtenir son type de désérialisation:

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

J’ai trouvé cette approche qui parsing JSON dans un object dynamic , il étend un DynamicObject et JavascriptConverter pour transformer la chaîne en un object.

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); } } 

Convertisseur

 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) })); } } } 

Utilisation ( exemple 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); } 

Cette méthode doit retourner true, sinon une erreur est générée. Vous pouvez par exemple lancer une erreur si une clé n’existe pas.

Renvoyer le résultat true et le result vidange renverra une valeur vide plutôt qu’une erreur.

 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); } 

Je pense juste que l’exemple complet serait utile. C’est l’exemple de ce problème.

 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 ssortingng 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 counsortinges.""},{""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 nusortingtion 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(ssortingng json) { using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json))) { var serialiser = new DataContractJsonSerializer(typeof(T)); return (T)serialiser.ReadObject(ms); } } public static ssortingng Serialize(T obj) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); using (MemoryStream ms = new MemoryStream()) { serializer.WriteObject(ms, obj); return Encoding.Default.GetSsortingng(ms.ToArray()); } } } [DataContract] public class GoogleSearchResults { [DataMember] public ResponseData responseData { get; set; } [DataMember] public ssortingng 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 ssortingng start { get; set; } [DataMember] public ssortingng label { get; set; } } [DataContract] public class Results { [DataMember] public ssortingng unescapedUrl { get; set; } [DataMember] public ssortingng url { get; set; } [DataMember] public ssortingng visibleUrl { get; set; } [DataMember] public ssortingng cacheUrl { get; set; } [DataMember] public ssortingng title { get; set; } [DataMember] public ssortingng titleNoFormatting { get; set; } [DataMember] public ssortingng content { get; set; } } 

J’ai essayé d’utiliser le code ci-dessus mais n’a pas fonctionné. La structure JSON renvoyée par Google est tellement différente et il y a un DataContractJsonSerializer.ReadObject() très important dans la fonction d’assistance: un appel à DataContractJsonSerializer.ReadObject() qui désérialise réellement les données JSON dans l’object.

Voici le code qui fonctionne 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(ssortingng 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 ssortingng GsearchResultClass { get; set; } public ssortingng unescapedUrl { get; set; } public ssortingng url { get; set; } public ssortingng visibleUrl { get; set; } public ssortingng cacheUrl { get; set; } public ssortingng title { get; set; } public ssortingng titleNoFormatting { get; set; } public ssortingng content { get; set; } } public class Page { public ssortingng start { get; set; } public int label { get; set; } } public class Cursor { public ssortingng resultCount { get; set; } public Page[] pages { get; set; } public ssortingng estimatedResultCount { get; set; } public int currentPageIndex { get; set; } public ssortingng moreResultsUrl { get; set; } public ssortingng 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; } } } 

Pour obtenir le contenu du premier résultat, faites:

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

Merci à tous pour votre aide. Ceci est ma version finale et cela fonctionne grâce à votre aide combinée! Je montre seulement les changements que j’ai faits, tout le rest est tiré du travail de Joe Chung

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

et

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

Demande Google API Google Map et parsing DirectionsResponse avec C #, remplacez json dans votre URL par xml et utilisez le code suivant pour transformer le résultat en un object de liste générique C # utilisable.

M’a pris un moment à faire. Mais la voici

 var url = Ssortingng.Format("http://maps.googleapis.com/maps/api/directions/xml?..."); var result = new System.Net.WebClient().DownloadSsortingng(url); var doc = XDocument.Load(new SsortingngReader(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(); 

J’espère que cela aidera quelqu’un.