2014-11-24 56 views
-2

在C#中,我得到了下面的字符串:如何在C#中分割該字符串並獲取第一個和最後一個經緯度?

[{"place_id":"92911594","licence":"Data \u00a9 OpenStreetMap contributors, ODbL 
1.0. http:\/\/www.openstreetmap.org\/copyright","osm_type":"way","osm_id":"12174 
8552","boundingbox":["-33.4480414","-33.4439319","-70.6448054","-70.6266944"],"l 
at":"-33.4460658","lon":"-70.6359402","display_name":"Mar\u00edn, Santiago, Prov 
incia de Santiago, XIII Regi\u00f3n Metropolitana de Santiago, 8331059, Chile"," 
class":"highway","type":"tertiary","importance":0.2,"address":{"road":"Mar\u00ed 
n","city":"Santiago","county":"Provincia de Santiago","state":"XIII Regi\u00f3n 
Metropolitana de Santiago","postcode":"8331059","country":"Chile","country_code" 
:"cl"}, 
"svg":"M -70.626694400000005 33.444016900000001 L 
-70.626853100000005 33.443931900000003 
-70.628024600000003 33.444266200000001 
-70.629083399999999 33.444542800000001 
-70.629957200000007 33.444764300000003 
-70.630791099999996 33.444983999999998 
-70.630836099999996 33.4449915 
-70.631027500000002 33.445031299999997 
-70.631102900000002 33.445044099999997 
-70.631955300000001 33.445186900000003 
-70.632777300000001 33.445343999999999 
-70.632846900000004 33.445356799999999 
-70.6329025 33.445371799999997 
-70.633783199999996 33.445576199999998 
-70.63402120 0000007 33.445624299999999 
-70.635066499999994 33.4458354 
-70.635940199999993 33.4460658 
-70.637316900000002 33.446388900000002 
-70.638039300000003 33.446610700000001 
-70.638551500000005 33.446730799999997 
-70.639778699999994 33.447019400000002 
-70.640489099999996 33.4471962 
-70.641005800000002 33.4473074 
-70.642197100000004 33.447521299999998 
-70.642251900000005 33.447535500000001 
-70.643692900000005 33.4478121 
-70.644805399999996 33.448041400000001 
"}] 

我需要得到和某些變量保存從SVG每個LAT-長着一雙。例如:

pair1 = "-70.626694400000005 33.444016900000001" 
pair2 = "-70.626853100000005 33.443931900000003" 
pairx = "" 

我tryng與拆分,但我不能得到使用正則表達式對。

+6

這串* *是一個JSON強,使用http://json2csharp.com/來爲您的JSON類,解析您的字符串到你的對象*(反序列化) *並且你會得到一個'svg'字符串類型的屬性。在空間上分割並得到'First'和'Last' – Habib 2014-11-24 16:56:03

+0

你會在你的數據之前總是得到''svg「:」M'嗎? – Tariq 2014-11-24 16:56:39

+0

你需要?很好!當你的編碼問題比你歡迎回來! – Vladimirs 2014-11-24 16:58:04

回答

0

你的字符串是一個json字符串。所以,你可以創建一個表示這個JSON數據

public class SomeClass{ 
    pubclis string place_id {get; set;} 
    public string license 
    // other members 
} 

然後一類,你可以使用一個JavascriptSerializer實例的JSON數據轉換爲SomeClass的實例。之後,您可以輕鬆檢索每個實例屬性。

參見:http://msdn.microsoft.com/en-US/en-en/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx

+0

我得到這個錯誤:解析值時遇到意外的字符:h。路徑'',第0行,位置0.使用此:var obj =(JObject)JsonConvert.DeserializeObject(「http://open.mapquestapi.com/nominatim/v1/search?&q=marin,+santiago&format=json&addressdetails=1&limit=1&polygon_svg=1&countrycodes=cl 「); var dict = obj.First.First.Children()。Cast () .ToDictionary(p => p.Name,p => p.Value); var dt =(string)dict [「c」]; var d =(double)dict [「g」]; Console.WriteLine(dt); – Patricio 2014-11-24 18:18:05

0

可能有更好的方法,但是:

public class Coord 
{ 
    public double latitude; 
    public double longitude; 
} 

var svg = "M -70.626694400000005 33.444016900000001 L -70.626853100000005 33.443931900000003 -70.628024600000003 33.444266200000001 -70.629083399999999 33.444542800000001 -70.629957200000007 33.444764300000003 -70.630791099999996 33.444983999999998 -70.630836099999996 33.4449915 -70.631027500000002 33.445031299999997 -70.631102900000002 33.445044099999997 -70.631955300000001 33.445186900000003 -70.632777300000001 33.445343999999999"; 

var formattedSvg = svg.Replace("M ", "").Replace("L ", ""); 

var values = formattedSvg.Split(' ').ToList().Select(v => Double.Parse(v)).ToArray(); 

var coords = new List<Coord>(); 

for (var index = 0; index < values.Length; index += 2) 
{ 
    coords.Add(new Coord 
    { 
     latitude = values[index], 
     longitude = values[index + 1] 
    }); 
} 
+0

謝謝@paul,但我怎樣才能獲得svg價值?因爲這個值來自json字符串。 – Patricio 2014-11-24 20:29:04

+0

我怎樣才能從經度和緯度得到的價值,並將其保存到一些變量? – Patricio 2014-11-24 20:32:35

0

由於這是JSON,你需要做的第一件事就是下載JSON.Net。接下來,創建一個類是這樣的:

public class MyClass 
{ 
    [JsonProperty(PropertyName = "svg")] 
    public string Svg { get; set;} 
} 

下,做到這一點:

string svg = (JsonConvert.DeserializeObject<MyClass>(myjsondata)).Svg; 

這會給你你需要SVG的字符串。接下來,你可以做這樣的事情:

var pairs = new List<string>(); 

    string[] strings = svg.Split(' '); 

    int ndx = 0; 

    while (ndx < strings.Length - 1) 
    { 
     if (strings[ndx].StartsWith("M") || strings[ndx].StartsWith("L")) 
     { 
      ndx++; 
      continue; 
     } 

     pairs.Add(strings[ndx] + " " + strings[ndx + 1]); 
     ndx += 2; 
    } 

    foreach (string s in pairs) 
    { 
     Console.WriteLine(s); 
    } 
+0

我得到下一個錯誤:解析值時遇到意外的字符:h。路徑'',第0行,位置0.怎麼了?謝謝!! – Patricio 2014-11-24 18:21:36

+0

@Patricio - 你什麼時候得到這個錯誤? – Icemanind 2014-11-24 18:25:17

+0

在這部分:string svg =(JsonConvert.DeserializeObject (url))。Svg; – Patricio 2014-11-24 18:28:24

相關問題