将此字符串解析为Guids或字符串数​​组

时间:2021-10-02 21:15:05

I'm working with Sitefinity and when you add a custom Tags attribute to a Page it results in the following string value:

我正在使用Sitefinity,当您向页面添加自定义Tags属性时,它会产生以下字符串值:

"[\"1f3560ca-84b9-6a87-9ce5-ff00009465c7\",\"893460ca-84b9-6a87-9ce5-ff00009465c7\"]"

Does anyone have a clever conversion method that can convert this string into an array of guids or strings?

有没有人有一个聪明的转换方法,可以将此字符串转换为guids或字符串数​​组?

I would write something that splits by , and removes the brackets... I just feel there must be a better way though but it doesn't come to mind.

我会写一些分裂的东西,并删除括号......我只觉得必须有一个更好的方法,但它不会浮现在脑海中。

3 个解决方案

#1


1  

You can use Microsoft JavaScriptSerializer class, which can help you turn a JSON string into objects.

您可以使用Microsoft JavaScriptSerializer类,它可以帮助您将JSON字符串转换为对象。

var serializer = new JavaScriptSerializer();
var deserializedResult = serializer.Deserialize<List<string>>(tags);

#2


0  

This is my current solution...

这是我目前的解决方案......

string tags = "[\"1f3560ca-84b9-6a87-9ce5-ff00009465c7\",\"893460ca-84b9-6a87-9ce5-ff00009465c7\"]";
return tags
    .Replace("[", "")
    .Replace("]", "")
    .Replace(" ", "")
    .Replace("\"", "")
    .Split(',')
    .Where(t =>
    {
        Guid g;
        return Guid.TryParse(t, out g);
    }).Select(t => new Guid(t)))

#3


0  

Have you tried casting it to TrackedList?

您是否尝试将其投射到TrackedList?

#1


1  

You can use Microsoft JavaScriptSerializer class, which can help you turn a JSON string into objects.

您可以使用Microsoft JavaScriptSerializer类,它可以帮助您将JSON字符串转换为对象。

var serializer = new JavaScriptSerializer();
var deserializedResult = serializer.Deserialize<List<string>>(tags);

#2


0  

This is my current solution...

这是我目前的解决方案......

string tags = "[\"1f3560ca-84b9-6a87-9ce5-ff00009465c7\",\"893460ca-84b9-6a87-9ce5-ff00009465c7\"]";
return tags
    .Replace("[", "")
    .Replace("]", "")
    .Replace(" ", "")
    .Replace("\"", "")
    .Split(',')
    .Where(t =>
    {
        Guid g;
        return Guid.TryParse(t, out g);
    }).Select(t => new Guid(t)))

#3


0  

Have you tried casting it to TrackedList?

您是否尝试将其投射到TrackedList?