Skip to main content
 首页 » 编程设计

c#之在 C# 中拆分字符串

2024年02月23日23飞鱼

我有一个 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; 

谢谢!

请您参考如下方法:

你可以这样创建一个类

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);