2013-06-19 42 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的列表,并且会调用该操作方法。 –

相关问题