[英]JSON Deserializing for Windows Phone
我正在嘗試反序列化以下JSON,但我真的不知道如何使用JSON.net來完成工作。 我正在使用C#和JSON.Net庫。
我的JSON如下:
{
"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": "toilets",
"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": "toilets",
"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": "toilets",
"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
。 這是一個有效的例子
string 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"": ""toilets"", ""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"": ""toilets"", ""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"": ""toilets"", ""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);
}
非動態版本
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 string type { get; set; }
public List<double> coordinates { get; set; }
}
public class Properties
{
public string osm_element { get; set; }
public string amenity { get; set; }
public string synthesized_name { get; set; }
public string osm_id { get; set; }
}
public class Geometry
{
public string type { get; set; }
public List<double> coordinates { get; set; }
}
public class Location
{
public string county { get; set; }
public string country { get; set; }
public string road { get; set; }
public string city { get; set; }
}
public class Feature
{
public int id { get; set; }
public Centroid centroid { get; set; }
public List<List<double>> bounds { get; set; }
public Properties properties { get; set; }
public Geometry geometry { get; set; }
public Location location { get; set; }
public string type { get; set; }
}
public class Properties2
{
public int code { get; set; }
public List<int> coordinate_order { get; set; }
}
public class Crs
{
public string type { get; set; }
public Properties2 properties { get; set; }
}
public class RootObject
{
public int found { get; set; }
public List<List<double>> bounds { get; set; }
public List<Feature> features { get; set; }
public string type { get; set; }
public Crs crs { get; set; }
}
干得好。
有一個用於從json http://json2csharp.com/生成C#類的工具。
首先創建一個適合JSONed對象的類。
然后,只需編寫JsonConvert.DeserializeObject<ClassName>(json)
其中ClassName
是您的類的名稱,而json
是包含您JSON的字符串。
您的數據結構非常復雜,因此為其創建一個類可能會有些復雜。 您可能需要簡化一下。
我將如何解決這個問題。
首先使用您的JSON並將其粘貼到: http : //jsonviewer.stack.hu/-這將為您提供如下視圖:
現在,從該視圖中顯示的對象中為每種類型的對象創建一個類-例如:
public class MainWrapper
{
public int found {get;set;}
public List<Bound> bounds {get;set;}
public List<Feature> features {get;set;}
public Crs crs {get;set;
}
最后,您現在可以使用一些Newtonsoft來反序列化為: JsonConvert.DeserializeObject<MainWrapper>(text)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.