0

假设我有一个具有以下属性的对象 Fish

Public Property ID As Integer
Public Property Name As String
Public Property Type As Integer
Public Property Age As Integer

我有一个看起来像这样的字符串:

"Fishes[0].ID=1&Fishes[0].Name=Fred&Fishes[0].Type=1&Fishes[0].Age=3&Fishes[1].ID=2&Fishes[1].Name=George&Fishes[1].Type=2&Fishes[1].Age=5&..."

有什么方法可以将我的字符串转换/转换/任何内容转换为 Fish 对象列表吗?我似乎找不到任何帮助。如果这会使事情变得更容易,我确实可以控制字符串的格式。

非常感谢

4

4 回答 4

1

您需要做的是解析输入字符串以查找Fishes[X].PROPNAME=VALUE模式。循环遍历字符串中找到的所有匹配项,并添加或设置 Dictionary 中的现有对象。用作X字典中的每个对象键。

创建鱼结构:

Structure Fish
    Public ID As String
    Public Name As String
    Public Type As Integer
    Public Age As Integer
End Structure

处理输入字符串的代码:

Dim Fishes As New Dictionary(Of String, Fish)
Dim m As Match = Regex.Match(str, "Fishes\[(?<key>\d+)]\.(?<prop>.+?)=(?<value>[^&]+)", RegexOptions.IgnoreCase)

Do While m.Success
    Dim key As String = m.Groups("key").Value.Trim.ToUpper
    Dim prop As String = m.Groups("prop").Value.Trim.ToUpper
    Dim value As String = m.Groups("value").Value

    ' if the key not yet exist in the Dictionary, create and add into it.
    If Not Fishes.ContainsKey(key) Then
        Fishes.Add(key, New Fish)
    End If

    Dim thisFish As Fish = Fishes(key) ' get the Fish object for this key

    ' determine the object property to set
    Select Case prop
        Case "ID" : thisFish.ID = value
        Case "NAME" : thisFish.Name = value
        Case "TYPE" : thisFish.Type = CInt(value)
        Case "AGE" : thisFish.Age = CInt(value)
    End Select

    Fishes(key) = thisFish ' since the Fish object is declared as Structure,
                           ' update the Dictionary item of key with the modified object.
                           ' If Fish is declared as Class, then this line is useless

    m = m.NextMatch()
Loop
于 2013-06-19T17:14:51.567 回答
0

尝试这个 ..

Structure Test
    Dim ID As String
    Dim Name As String
    Dim Type As Integer
    Dim Age As Integer
End Structure

在您的按钮单击事件中..

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Fishes As New List(Of Test)

    Dim Fish As New Test
    Fish.ID = "GF"
    Fish.Name = "Gold Fish"
    Fish.Age = 1
    Fish.Type = 1
    Fishes.Add(Fish)

    MsgBox(Fishes(0).Name)

End Sub
于 2013-06-19T14:43:19.973 回答
0

在您提到的评论中,这是通过 ASP.Net MVC 实现的。ModelBinder 将为您做所有事情。

public ActionResult UpdateFish(IList<Fish> listOfFish)
    {
        //the ModelBinder will figure out that the user has posted a list of Fish instances.
        //do something with listOfFish
        return View(listOfFish);
    }
于 2013-06-19T18:09:41.053 回答
0

这是vb.net转换的

Public Partial Class test
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs)


        If Not IsPostBack Then
        End If

    End Sub
    Public Sub MYtest()
        Dim ls As New List(Of Fish)()
        Dim f As New Fish()
        f.ID = 1
        f.Name = "My name"
        f.Type = "My type"
        f.Age = 20
        ls.Add(f)
    End Sub
End Class
Public Class Fish
    Public Property ID() As Integer
        Get
            Return m_ID
        End Get
        Set
            m_ID = Value
        End Set
    End Property
    Private m_ID As Integer
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
    Public Property Type() As String
        Get
            Return m_Type
        End Get
        Set
            m_Type = Value
        End Set
    End Property
    Private m_Type As String
    Public Property Age() As Integer
        Get
            Return m_Age
        End Get
        Set
            m_Age = Value
        End Set
    End Property
    Private m_Age As Integer
End Class
于 2013-06-19T14:52:40.697 回答