2013-02-05 64 views

回答

2

這應該工作:

班反序列化JSON字符串

public class PlaceInfo 
{ 
    public string place_id { get; set; } 
    public string licence { get; set; } 
    public string osm_type { get; set; } 
    public string osm_id { get; set; } 
    public List<string> boundingbox { get; set; } 
    public string lat { get; set; } 
    public string lon { get; set; } 
    public string display_name { get; set; } 
    public string @class { get; set; } 
    public string type { get; set; } 
    public double importance { get; set; } 
    public string icon { get; set; } 
} 

這是代碼從網站上的信息: 格式是JSON,我使用的是json serializor的c#

using System.Runtime.Serialization; using System.Runtime.Serialization.Json;

WebClient webClient = new WebClient(); 
string jsonString = webClient.DownloadString("http://nominatim.openstreetmap.org/search?city=%22new%20york%22&format=json"); 

//load into memorystream 
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString))) 
{ 
    //parse 
    var ser = new DataContractJsonSerializer(typeof(PlaceInfo[])); 
    PlaceInfo[] obj = (PlaceInfo[])ser.ReadObject(ms); 
} 

該數組obj現在已在所有的地方找到那個名字。例如jsut首先找到obj [0] .lon和obj [0] .lat

+0

感謝您的回答:) – sunny

2
WebClient webClient = new WebClient(); 
string xml = webClient.DownloadString("http://nominatim.openstreetmap.org/search?city=%22new%20york%22&format=xml"); 
+0

你有沒有使用過這個函數來獲取緯度和lang度? ? – sunny