VB.net怎么能拆分这个?

时间:2013-12-14 01:04:49

标签: regex vb.net string split

我试图在VB.net(owner_id)中拆分此代码,这是数据字符串。

yt.setConfig('DISTILLER_CONFIG', {"signin_url": "https:\/\/accounts.google.com\/ServiceLogin?hl=da\u0026continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26app%3Ddesktop%26feature%3Dcomments%26hl%3Dda%26next%3D%252Fall_comments%253Fv%253DZNW_uQaYfB0\u0026uilel=3\u0026passive=true\u0026service=youtube", "host_override": "https:\/\/plus.googleapis.com", "query": "http:\/\/www.youtube.com\/watch?v=ZNW_uQaYfB0", "channel_id": "UCe4LM_eKc9ywRmVuBm5pjQg", "first_time_comment_promo": false, "privacy_setting": "PUBLIC", "visible": true, "pinned_activity": null, "page_size": 100, "owner_id": "e4LM_eKc9ywRmVuBm5pjQg", "reauth": false, "video_id": "ZNW_uQaYfB0"});

到目前为止,我已尝试过此代码,但它无法正常工作。已经声明了owner_id字符串..

owner_id = (Split(data, """owner_id"": """)(1).Split("""")(0)

但它不起作用。

编辑:

如何将JSON选择为我想从这些脚本中拆分的字符串..?

http://pastebin.com/50bxc83T

2 个答案:

答案 0 :(得分:0)

我建议使用Regex

"owner_id": "([\d\w]*)"

仅当您确实要解析此单个键/值对时。如果应该提取更多,我宁愿考虑提取JSON并以正常方式反序列化它。

工作示例:

Dim regex As Regex = New Regex("""owner_id"": ""([\d\w]*)""")
Dim match As Match = regex.Match("...here your string...")
If match.Success Then
   Console.WriteLine(match.Groups(1).Value)
End If

如果有多个部分,例如yt.Config(...),您可以将所需的一个标识符包含在正则表达式中,例如:

Dim regex As Regex = New Regex("yt.setConfig\('DISTILLER_CONFIG'.*""owner_id"": ""([\d\w]*)""")

答案 1 :(得分:0)

这是JSON,字符串拆分不是一个好主意,而是你应该使用Json.NET将JSON反序列化为类对象,如下所示:

Public Class DistillerConfigResults
    Public Property DISTILLER_CONFIG As DistillerConfig
End Class

Public Class DistillerConfig
    Public Property signin_url As String
    Public Property host_override As String
    Public Property query As String
    Public Property signin_url As String
    Public Property channel_id As String
    Public Property first_time_comment_promo As Boolean
    Public Property privacy_setting As String
    Public Property visible As Boolean
    Public Property pinned_activity As Object
    Public Property page_size As Integer
    Public Property owner_id As String
    Public Property reauth As Boolean
    Public Property video_id As String
End Class

现在您可以将JSON反序列化为类对象,如下所示:

Dim a As DistillerConfigResults = JsonConvert.DeserializeObject(Of DistillerConfigResults)(jsonString)