2011-06-28 271 views
1

我有一個string越來越貼到我的MVC的行動看起來像這樣:分割字符串

[{"property":"radius","value":"twentyfive"},{"property":"latlng","value":"40.036218,-75.51381100000003"}] 

我需要的twentyfive在這種情況下,半徑值,但可以是任何東西,而且每個緯度和經度數,所以它們在這裏是40.036218-75.51381100000003

所以像:

string filter = "[{\"property\":\"radius\",\"value\":\"twentyfive\"},{\"property\":\"latlng\",\"value\":\"40.036218,-75.51381100000003\"}]"; 
string radius = //whatever i need to do; 
double lat = //whatever i need to do; 
double lng = //whatever i need to do; 

謝謝!

+4

胡克自己了一些JSON.NET,只是反序列化JSON字符串轉換爲對象。 –

回答

4

您可以創建一個類這樣

public class PropertyValue 
{ 
    public string property {get; set;} 
    public string value {get; set;} 
} 

,並使用JavaScriptSerializer

string inputString = @"[{""property"":""radius"",""value"":""twentyfive""},{""property"":""latlng"",""value"":""40.036218,-75.51381100000003""}]"; 
IList<PropertyValue> propertyValueList = new JavaScriptSerializer() 
     .Deserialize<IList<PropertyValue>>(inputString); 
Console.WriteLine(propertyValueList.Single(p => p.property == "radius").value);