2011-06-28 211 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);