2013-06-19 77 views
0

說我有一個屬性的對象魚像字符串轉換爲自定義的對象列表

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&..." 

是否有轉換的任何方式/鑄造/無論我的字符串成魚列表的對象?我似乎無法找到任何幫助。如果能讓事情變得簡單,我對字符串的格式有一些控制。

非常感謝

+0

進行擴展方法 – skhurams

+0

使用VAR LS =新名單然後進行擴展 – skhurams

+0

@skhurams ..你爲什麼不使一個答案? – matzone

回答

1

你什麼需要做的是將輸入字符串解析爲lo好吧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 
+0

這正是我所要求的!非常感謝!我不確定沒有更好的方法來做到這一點,讓ModelBinder獲得幫助或者其他方法,但在此期間這是可行的(即使它可能不理想)。再次感謝! – Valuk

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 
+0

嗨,對不起,我對此很陌生,我不是真的很確定我遵循你的建議。我的問題存在於一些代碼的中間,沒有按鈕 - 一個方法傳遞一個字符串(請參閱問題),然後我需要構造一個Fish的列表以供在方法中使用。我將如何使用你的答案呢?謝謝 – Valuk

+0

@Valuk ..你使用winform還是什麼? – matzone

+0

我正在使用ASP.NET MVC和VB,但在我遇到問題時,它只是試圖將字符串轉換爲對象列表的情況,是不是隻是VB相關?對任何困惑道歉,我自己很困惑。 – Valuk

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 
+0

對不起,我不確定我理解,我將如何使用它將我的字符串轉換爲Fish列表? – Valuk

+0

看mytest功能 – skhurams

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); 
    } 
+0

顯然(這是由其他人完成的)我正在使用的字符串是從視圖上的模型(在JavaScript中)構建的,然後作爲字符串傳遞給iframe,然後從那裏發佈。有沒有一種方法來欺騙ModelBinder,它來自一個模型?也許我應該在發佈_that_之前重建iframe中的模型? – Valuk

+0

你不必做任何欺騙。 ModelBinder會發現用戶已經發布了Fish的列表,並且會調用該操作方法。 –

相關問題