所以我看了很多帖子,但我仍然在努力将这个 JSON 对象序列化为类。 JSON结构是这样的
{"value":{"HashTag":"12342345636","companyname":"my test company","LeadDetail":{"id":"1","firstname":"john","lastname":"clark","email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="442129252f0429252d6a272b29" rel="noreferrer noopener nofollow">[email protected]</a>","phone":"9874534444"}}}
我的类结构如下:
<Serializable> _
Public Class LeadDetailCall
Public Property Hash() As String
Get
Return m_Hash
End Get
Set(value As String)
m_Hash = value
End Set
End Property
Private m_Hash As String = ""
Public Property CompanyName() As String
Get
Return _CompanyName
End Get
Set(value As String)
_CompanyName = value
End Set
End Property
Private _CompanyName As String = ""
Public Property Details() As List(Of LeadDetail)
Get
Return _Details
End Get
Set(ByVal value As List(Of LeadDetail))
_Details = value
End Set
End Property
Private _Details As List(Of LeadDetail)
End Class
<Serializable> _
Public Class LeadDetail
Private _id As String = ""
Private _firstname As String = ""
Private _lastname As String = ""
Private _email As String = ""
Private _phone As String = ""
Public Property id() As String
Get
Return _id
End Get
Set(value As String)
_id = value
End Set
End Property
Public Property firstname() As String
Get
Return _firstname
End Get
Set(ByVal value As String)
_firstname = value
End Set
End Property
Public Property lastname() As String
Get
Return _lastname
End Get
Set(ByVal value As String)
_lastname = value
End Set
End Property
Public Property email() As String
Get
Return _email
End Get
Set(ByVal value As String)
_email = value
End Set
End Property
Public Property phone() As String
Get
Return _phone
End Get
Set(ByVal value As String)
_phone = value
End Set
End Property
End Class
我这样称呼:
<WebMethod()> _
Public Function SendLeadDetails(ByVal value As Object) As UpdateResponse
Dim CurCall As LeadDetailCall = JsonConvert.DeserializeObject(Of LeadDetailCall)(value)
End Function
我尝试了什么?使用 JavaScriptSerializer,使用 http://jsontodatacontract.azurewebsites.net/ 爬行 stackoverflow 作为示例。事情太多,让我此时心慌意乱。所以我不断收到以下无效转换异常错误。
Conversion from type 'Dictionary(Of String,Object)' to type 'String' is not valid.
如果有人可以帮助我,我将非常感激:)
最佳答案
提供的 JSON 仅包含一个元素,因此它将生成一个包含一个元素的集合(字典)。我添加了一个“项目”以确保下面的代码有效并用于说明目的。正确的缩进使事情更容易理解:
{
"foo": {
"HashTag": "12342345636",
"companyname": "my test company",
"LeadDetail": {
"id": "1",
"firstname": "ziggy",
"lastname": "clark",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="751018141e3518141c5b161a18" rel="noreferrer noopener nofollow">[email protected]</a>",
"phone": "9874534444"
}
},
"bar": {
"HashTag": "02342345636",
"companyname": "my test company2",
"LeadDetail": {
"id": "1",
"firstname": "john",
"lastname": "clark",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a6aea2a883aea2aaeda0acae" rel="noreferrer noopener nofollow">[email protected]</a>",
"phone": "1874534444"
}
}
}
从最深的缩进开始,很容易看到带有 {ID、FirstName 等)的 LeadDetail
类。如果有多个项目(如我的),就会重复这种情况。
然后是带有少量数据的“foo”和“bar”对象以及一个 LeadDetail
对象。当您使用任何机器人时,它们都会为每个机器人创建一个类。我的将被命名为“foo”和“bar”,但其他方面是相同的。在下面的代码中,我将其压缩为一个名为“Item”的类。然后,您可以将它们(Foo 和 Bar)视为 Dictionary(of String, Item)
,其中它们的名称是键。
但是还有一个不太明显的类/类型:最外层的{..}
。机器人工具将创建一个名为“Example”或“RootObject”的类。您不需要它或想要它作为字典:
' modified from VS's EDIT -> Paste Special -> JSON as Classes
Public Class Item
Public Property HashTag As String
Public Property companyname As String
Public Property LeadDetail As Leaddetail
End Class
Public Class Leaddetail
Public Property id As String
Public Property firstname As String
Public Property lastname As String
Public Property email As String
Public Property phone As String
End Class
然后是代码:
Dim jstr As String = ...
' use the Item/Value class not the container
Dim myJ = JsonConvert.DeserializeObject(Of Dictionary(Of String, Item))(jstr)
' print some of the data
For Each kvp As KeyValuePair(Of String, Item) In myJ
Console.WriteLine("key {0}, CompName {1}, Contact: {2}", kvp.Key,
kvp.Value.companyname,
kvp.Value.LeadDetail.firstname)
Next
输出:
key: foo, CompName: my test company, Contact: ziggy
key: bar, CompName: my test company2, Contact: john
如果你在原始 JSON 上运行它,你会得到一个 1 的字典。
关于asp.net - VB.net反序列化,JSON从类型 'Dictionary(Of String,Object)'到类型 'String'的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29849015/