JSON Deserializing pour Windows Phone

J’essaie de désérialiser le JSON suivant, mais je ne sais vraiment pas comment utiliser JSON.net pour faire le travail. J’utilise la bibliothèque C # et JSON.Net.

Mon JSON est comme suit:

{ "found": 3, "bounds": [ [ -43.54919, 172.62148 ], [ -43.54487, 172.63654 ] ], "features": [ { "id": 15342454, "centroid": { "type": "POINT", "coordinates": [ -43.54779, 172.62148 ] }, "bounds": [ [ -43.54779, 172.62148 ], [ -43.54779, 172.62148 ] ], "properties": { "osm_element": "node", "amenity": "canvasts", "synthesized_name": "Toilets", "osm_id": "502884303" }, "geometry": { "type": "POINT", "coordinates": [ -43.54779, 172.62148 ] }, "location": { "county": "Canterbury", "country": "New Zealand", "road": "Sommerset Crescent", "city": "Christchurch" }, "type": "Feature" }, { "id": 19313858, "centroid": { "type": "POINT", "coordinates": [ -43.54919, 172.63654 ] }, "bounds": [ [ -43.54919, 172.63654 ], [ -43.54919, 172.63654 ] ], "properties": { "osm_element": "node", "amenity": "canvasts", "synthesized_name": "Toilets", "osm_id": "676225633" }, "geometry": { "type": "POINT", "coordinates": [ -43.54919, 172.63654 ] }, "location": { "county": "Canterbury", "country": "New Zealand", "road": "Colombo Street", "city": "Christchurch" }, "type": "Feature" }, { "id": 22536275, "centroid": { "type": "POINT", "coordinates": [ -43.54487, 172.63632 ] }, "bounds": [ [ -43.54487, 172.63632 ], [ -43.54487, 172.63632 ] ], "properties": { "osm_element": "node", "amenity": "canvasts", "synthesized_name": "Toilets", "osm_id": "864392689" }, "geometry": { "type": "POINT", "coordinates": [ -43.54487, 172.63632 ] }, "location": { "county": "Canterbury", "country": "New Zealand", "road": "Wordsworth Street", "city": "Christchurch" }, "type": "Feature" } ], "type": "FeatureCollection", "crs": { "type": "EPSG", "properties": { "code": 4326, "coordinate_order": [ 0, 1 ] } } } 

Il n’est pas nécessaire de déclarer beaucoup de classes minuscules pour désérialiser. Juste utiliser dynamic . Voici un exemple de travail

 ssortingng jsonstr = @"{""found"": 3, ""bounds"": [[-43.54919, 172.62148], [-43.54487, 172.63654]], ""features"": [{""id"": 15342454,""centroid"": {""type"":""POINT"",""coordinates"":[-43.54779, 172.62148]},""bounds"": [[-43.54779, 172.62148], [-43.54779, 172.62148]],""properties"": {""osm_element"": ""node"", ""amenity"": ""canvasts"", ""synthesized_name"": ""Toilets"", ""osm_id"": ""502884303""},""geometry"": {""type"":""POINT"",""coordinates"":[-43.54779, 172.62148]},""location"": {""county"": ""Canterbury"", ""country"": ""New Zealand"", ""road"": ""Sommerset Crescent"", ""city"": ""Christchurch""},""type"": ""Feature""},{""id"": 19313858,""centroid"": {""type"":""POINT"",""coordinates"":[-43.54919, 172.63654]},""bounds"": [[-43.54919, 172.63654], [-43.54919, 172.63654]],""properties"": {""osm_element"": ""node"", ""amenity"": ""canvasts"", ""synthesized_name"": ""Toilets"", ""osm_id"": ""676225633""},""geometry"": {""type"":""POINT"",""coordinates"":[-43.54919, 172.63654]},""location"": {""county"": ""Canterbury"", ""country"": ""New Zealand"", ""road"": ""Colombo Street"", ""city"": ""Christchurch""},""type"": ""Feature""},{""id"": 22536275,""centroid"": {""type"":""POINT"",""coordinates"":[-43.54487, 172.63632]},""bounds"": [[-43.54487, 172.63632], [-43.54487, 172.63632]],""properties"": {""osm_element"": ""node"", ""amenity"": ""canvasts"", ""synthesized_name"": ""Toilets"", ""osm_id"": ""864392689""},""geometry"": {""type"":""POINT"",""coordinates"":[-43.54487, 172.63632]},""location"": {""county"": ""Canterbury"", ""country"": ""New Zealand"", ""road"": ""Wordsworth Street"", ""city"": ""Christchurch""},""type"": ""Feature""}], ""type"": ""FeatureCollection"", ""crs"": {""type"": ""EPSG"", ""properties"": {""code"": 4326, ""coordinate_order"": [0, 1]}}}"; dynamic json = JsonConvert.DeserializeObject(jsonstr); foreach (var feature in json.features) { Console.Write("{0},{1} - {2},{3} : ", feature.bounds[0][0], feature.bounds[0][1], feature.bounds[1][0], feature.bounds[1][1]); Console.WriteLine("{0} {1} {2} {3}", feature.location.country, feature.location.county, feature.location.city, feature.location.road); } 

Version non dynamic

 JObject json = (JObject)JsonConvert.DeserializeObject(jsonstr); foreach (var feature in json["features"]) { Console.Write("{0},{1} - {2},{3} : ", feature["bounds"][0][0], feature["bounds"][0][1], feature["bounds"][1][0], feature["bounds"][1][1]); Console.WriteLine("{0} {1} {2} {3}", feature["location"]["country"], feature["location"]["county"], feature["location"]["city"], feature["location"]["road"]); } 
 public class Centroid { public ssortingng type { get; set; } public List coordinates { get; set; } } public class Properties { public ssortingng osm_element { get; set; } public ssortingng amenity { get; set; } public ssortingng synthesized_name { get; set; } public ssortingng osm_id { get; set; } } public class Geometry { public ssortingng type { get; set; } public List coordinates { get; set; } } public class Location { public ssortingng county { get; set; } public ssortingng country { get; set; } public ssortingng road { get; set; } public ssortingng city { get; set; } } public class Feature { public int id { get; set; } public Centroid centroid { get; set; } public List> bounds { get; set; } public Properties properties { get; set; } public Geometry geometry { get; set; } public Location location { get; set; } public ssortingng type { get; set; } } public class Properties2 { public int code { get; set; } public List coordinate_order { get; set; } } public class Crs { public ssortingng type { get; set; } public Properties2 properties { get; set; } } public class RootObject { public int found { get; set; } public List> bounds { get; set; } public List features { get; set; } public ssortingng type { get; set; } public Crs crs { get; set; } } 

Voici.

Il existe un outil pour générer des classes C # à partir de json http://json2csharp.com/ .

Commencez par créer une classe qui convient à l’object JSONed.
Ensuite, écrivez simplement JsonConvert.DeserializeObject(json)
ClassName est le nom de votre classe et JSON une chaîne contenant votre code JSON.

Vous avez une structure de données assez complexe, la création d’une classe peut donc être un peu complexe. Vous voudrez peut-être simplifier un peu.

Comment je voudrais aborder cette ….

Prenez d’abord votre JSON et collez-le dans: http://jsonviewer.stack.hu/ – ceci vous donne une vue comme:

JSON

Maintenant, à partir des objects affichés dans cette vue, créez une classe pour chaque type d’object – par exemple:

 public class MainWrapper { public int found {get;set;} public List bounds {get;set;} public List features {get;set;} public Crs crs {get;set; } 

Enfin, vous pouvez maintenant utiliser Newtonsoft pour désérialiser en tant que: JsonConvert.DeserializeObject(text)