我能够让代码在C#中工作,但是我在Visual Basic中编写程序。我通过转换器运行代码并得到了这个:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim html As String = String.Empty
Dim url As String = "https://crm.zoho.com/crm/private/json/Contacts/searchRecords?authtoken=0e0dd5f8153bc0a1299766b32d09e4f7&&scope=crmapi&fromIndex=1&toIndex=200&criteria=(Store:3110)"
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
request.AutomaticDecompression = DecompressionMethods.GZip
Using response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
Using stream As Stream = response.GetResponseStream()
Using reader As New StreamReader(stream)
html = reader.ReadToEnd()
End Using
End Using
End Using
Dim loContactList As New List(Of Contact)()
Dim loContact As Contact = Nothing
Dim respone As ResponseActual = JsonConvert.DeserializeObject(Of ResponseActual)(html)
For Each row var In respone.Response.Result.Contacts.Row
loContact = New Contact()
Dim rowItem = row.FL.ToList()
Try
loContact.ContactID = rowItem.Where(Of FL)(Function(s, t) s.Val = "CONTACTID").[Select](Function(x) x.Content).[Single]()
Catch
End Try
Try
loContact.OwnerID = rowItem.Where(Of FL)(Function(s, t) s.Val = "SMOWNERID").[Select](Function(x) x.Content).[Single]()
Catch
End Try
Try
loContact.ContactOwner = rowItem.Where(Of FL)(Function(s, t) s.Val = "Contact Owner").[Select](Function(x) x.Content).[Single]()
Catch
End Try
Try
loContact.FirstName = rowItem.Where(Of FL)(Function(s, t) s.Val = "First Name").[Select](Function(x) x.Content).[Single]()
Catch
End Try
Try
loContact.LastName = rowItem.Where(Of FL)(Function(s, t) s.Val = "Last Name").[Select](Function(x) x.Content).[Single]()
Catch
End Try
Try
loContact.Email = rowItem.Where(Of FL)(Function(s, t) s.Val = "Email").[Select](Function(x) x.Content).[Single]()
Catch
End Try
Try
loContact.CreatorID = rowItem.Where(Of FL)(Function(s, t) s.Val = "SMCREATORID").[Select](Function(x) x.Content).[Single]()
Catch
End Try
Try
loContact.CreatedBy = rowItem.Where(Of FL)(Function(s, t) s.Val = "Created By").[Select](Function(x) x.Content).[Single]()
Catch
End Try
Try
loContact.ModifiedByID = rowItem.Where(Of FL)(Function(s, t) s.Val = "MODIFIEDBY").[Select](Function(x) x.Content).[Single]()
Catch
End Try
Try
loContact.ModifiedBy = rowItem.Where(Of FL)(Function(s, t) s.Val = "Modified By").[Select](Function(x) x.Content).[Single]()
Catch
End Try
Try
loContact.CreatedTime = Convert.ToDateTime(rowItem.Where(Of FL)(Function(s, t) s.Val = "Created Time").[Select](Function(x) x.Content).[Single]())
Catch
End Try
Try
loContact.ModifiedTime = Convert.ToDateTime(rowItem.Where(Of FL)(Function(s, t) s.Val = "Modified Time").[Select](Function(x) x.Content).[Single]())
Catch
End Try
Try
loContact.LastActivityTime = Convert.ToDateTime(rowItem.Where(Of FL)(Function(s, t) s.Val = "Last Activity Time").[Select](Function(x) x.Content).[Single]())
Catch
End Try
ListBox1.Items.Add(loContact.FirstName + " " + loContact.LastName)
loContactList.Add(loContact)
Next
End Sub
End Class
Public Class Contact
<JsonProperty(PropertyName:="CONTACTID")>
Public Property ContactID() As String
Get
Return m_ContactID
End Get
Set
m_ContactID = Value
End Set
End Property
Private m_ContactID As String
<JsonProperty(PropertyName:="SMOWNERID")>
Public Property OwnerID() As String
Get
Return m_OwnerID
End Get
Set
m_OwnerID = Value
End Set
End Property
Private m_OwnerID As String
<JsonProperty(PropertyName:="Contact Owner")>
Public Property ContactOwner() As String
Get
Return m_ContactOwner
End Get
Set
m_ContactOwner = Value
End Set
End Property
Private m_ContactOwner As String
<JsonProperty(PropertyName:="First Name")>
Public Property FirstName() As String
Get
Return m_FirstName
End Get
Set
m_FirstName = Value
End Set
End Property
Private m_FirstName As String
<JsonProperty(PropertyName:="Last Name")>
Public Property LastName() As String
Get
Return m_LastName
End Get
Set
m_LastName = Value
End Set
End Property
Private m_LastName As String
<JsonProperty(PropertyName:="Email")>
Public Property Email() As String
Get
Return m_Email
End Get
Set
m_Email = Value
End Set
End Property
Private m_Email As String
<JsonProperty(PropertyName:="SMCREATORID")>
Public Property CreatorID() As String
Get
Return m_CreatorID
End Get
Set
m_CreatorID = Value
End Set
End Property
Private m_CreatorID As String
<JsonProperty(PropertyName:="Created By")>
Public Property CreatedBy() As String
Get
Return m_CreatedBy
End Get
Set
m_CreatedBy = Value
End Set
End Property
Private m_CreatedBy As String
<JsonProperty(PropertyName:="MODIFIEDBY")>
Public Property ModifiedByID() As String
Get
Return m_ModifiedByID
End Get
Set
m_ModifiedByID = Value
End Set
End Property
Private m_ModifiedByID As String
<JsonProperty(PropertyName:="Modified By")>
Public Property ModifiedBy() As String
Get
Return m_ModifiedBy
End Get
Set
m_ModifiedBy = Value
End Set
End Property
Private m_ModifiedBy As String
<JsonProperty(PropertyName:="Created Time")>
Public Property CreatedTime() As DateTime
Get
Return m_CreatedTime
End Get
Set
m_CreatedTime = Value
End Set
End Property
Private m_CreatedTime As DateTime
<JsonProperty(PropertyName:="Modified Time")>
Public Property ModifiedTime() As DateTime
Get
Return m_ModifiedTime
End Get
Set
m_ModifiedTime = Value
End Set
End Property
Private m_ModifiedTime As DateTime
<JsonProperty(PropertyName:="Last Activity Time")>
Public Property LastActivityTime() As DateTime
Get
Return m_LastActivityTime
End Get
Set
m_LastActivityTime = Value
End Set
End Property
Private m_LastActivityTime As DateTime
End Class
Public Class ResponseActual
<JsonProperty("response")>
Public Property Response() As Response2
Get
Return m_Response
End Get
Set
m_Response = Value
End Set
End Property
Private m_Response As Response2
End Class
Public Class Response2
<JsonProperty("result")>
Public Property Result() As Result
Get
Return m_Result
End Get
Set
m_Result = Value
End Set
End Property
Private m_Result As Result
<JsonProperty("uri")>
Public Property Uri() As String
Get
Return m_Uri
End Get
Set
m_Uri = Value
End Set
End Property
Private m_Uri As String
End Class
Public Class Result
<JsonProperty("Contacts")>
Public Property Contacts() As Contacts
Get
Return m_Contacts
End Get
Set
m_Contacts = Value
End Set
End Property
Private m_Contacts As Contacts
End Class
Public Class Contacts
<JsonProperty("row")>
Public Property Row() As IList(Of Row)
Get
Return m_Row
End Get
Set
m_Row = Value
End Set
End Property
Private m_Row As IList(Of Row)
End Class
Public Class Row
<JsonProperty("no")>
Public Property No() As String
Get
Return m_No
End Get
Set
m_No = Value
End Set
End Property
Private m_No As String
<JsonProperty("FL")>
Public Property FL() As IList(Of FL)
Get
Return m_FL
End Get
Set
m_FL = Value
End Set
End Property
Private m_FL As IList(Of FL)
End Class
Public Class FL
<JsonProperty("content")>
Public Property Content() As String
Get
Return m_Content
End Get
Set
m_Content = Value
End Set
End Property
Private m_Content As String
<JsonProperty("val")>
Public Property Val() As String
Get
Return m_Val
End Get
Set
m_Val = Value
End Set
End Property
Private m_Val As String
End Class
我被困在这条线上: For Each row var in respone.Response.Result.Contacts.Row
我知道我不能从C#获得var,但我无法弄清楚如何使这项工作。
答案 0 :(得分:1)
您拥有的代码可以简化很多。 JSON类没有问题,只有一个例外:
Public Class Row
Public Property no As String
Public Property FL As FL()
End Class
由于它已修复,因此数组就可以了。
但是可以更改联系人类别以完成一些工作:
Public Class Contact
Public Property ContactID As String
Public Property OwnerID As String
Public Property ContactOwner As String
Public Property FirstName As String
Public Property LastName As String
Public Property Email As String
Public Property CreatorID As String
Public Property CreatedBy As String
Public Property ModifiedByID As String
Public Property ModifiedBy As String
Public Property CreatedTime As DateTime
Public Property ModifiedTime As DateTime
Public Property LastActivityTime As DateTime
Public Sub New(items As FL())
ContactID = GetValue("CONTACTID", items)
ContactOwner = GetValue("SMOWNERID", items)
FirstName = GetValue("First Name", items)
LastName = GetValue("Last Name", items)
...
Dim tmp = GetValue("Created Time", items)
CreatedTime = Convert.ToDateTime(tmp)
tmp = GetValue("Modified Time", items)
ModifiedTime = Convert.ToDateTime(tmp)
End Sub
' the key corresponds to `val` in the json
Private Function GetValue(key As String, items As FL()) As String
Dim value = items.FirstOrDefault(Function(q) q.val = key)
If value Is Nothing Then
Return ""
Else
Return value.content
End If
End Function
Public Overrides Function ToString() As String
Return String.Format("{0}, {1}", LastName, FirstName)
End Function
End Class
FL()
并使用它来查找prop值本身GetValue()
辅助函数可以实际查找,因此Dont Repeat Yourself ToString()
覆盖提供ListBox
的显示,创建的列表用作DataSource
,因此您无需为控件创建数据位副本。 / LI>
另外,我不确定您似乎想要使用匹配的属性映射是否正确。例如,CONTACTID
和Contact Owner
似乎混淆了,对于某些事情,我无法告诉他们应该来自何处SMCREATORID
。
用法:
' form/class level list
Private contacts As List(Of Contact)
其他地方:
' using NEW here, empties the list from previous runs
contacts = New List(Of Contact)
Dim jstr = ...the json from whereever ...
Dim jobj = JsonConvert.DeserializeObject(Of ResponseActual)(jstr)
For Each r As Row In jobj.response.result.Contacts.row
' each contact item creates itself
' from the FL array
contacts.Add(New Contact(r.FL))
Next
ListBox1.DataSource = contacts