VB.net 反序列化,JSON 从类型 'Dictionary(Of String,Object)' 到类型 'String' 的转换
VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type 'String'
所以我看了很多帖子,但我仍然在努力将这个 JSON 对象序列化为 classes。 JSON结构是这样的
{"value":{"HashTag":"12342345636","companyname":"my test company","LeadDetail":{"id":"1","firstname":"john","lastname":"clark","email":"emak@mai.com","phone":"9874534444"}}}
我的class结构如下:
<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/抓取Whosebug作为例子。这么多的事情,在这一点上我很慌张。所以我不断收到以下无效转换异常错误。
Conversion from type 'Dictionary(Of String,Object)' to type 'String' is not valid.
如果有人能帮助我,我将不胜感激:)
您的 类 似乎与您的 Json 不符。
使用 http://jsonutils.com/,建议您的 类 应如下所示:
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
Public Class Value
Public Property HashTag As String
Public Property companyname As String
Public Property LeadDetail As LeadDetail
End Class
Public Class LeadDetailCall
Public Property value As Value
End Class
提供的JSON只有一个元素,所以会得到一个集合(字典)。我添加了一个 "item" 以确保下面的代码有效并用于说明目的。适当的缩进使事情更容易理解:
{
"foo": {
"HashTag": "12342345636",
"companyname": "my test company",
"LeadDetail": {
"id": "1",
"firstname": "ziggy",
"lastname": "clark",
"email": "emak@mai.com",
"phone": "9874534444"
}
},
"bar": {
"HashTag": "02342345636",
"companyname": "my test company2",
"LeadDetail": {
"id": "1",
"firstname": "john",
"lastname": "clark",
"email": "emak@mai.com",
"phone": "1874534444"
}
}
}
从最深的缩进开始,很容易看到 LeadDetail
class 带有 {ID, FirstName, etc)。对于不止一项(如我的),这将重复。
然后是"foo"和"bar"对象,有一点数据和一个LeadDetail
对象。当您使用任何机器人时,它们都会为每个机器人创建一个 class。我的将被命名为 "foo" 和 "bar",但在其他方面是相同的。在下面的代码中,我将其压缩为一个名为 "Item" 的 class。然后您可以将它们(Foo 和 Bar)视为 Dictionary(of String, Item)
,其中它们的名称是键。
但是还有一个,不太明显class/Type:最外面{..}
。机器人工具将创建一个名为 "Example" 或 "RootObject" 的 class。你不需要它或想要它作为字典:
' 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 上,你会得到一本字典。
@Keith Beard 之前在这个问题中说过需要序列化,然后反序列化到 类。我从 ajax jquery 调用网络方法。
<System.Web.Services.WebMethod()>
Public Shared Function InsertObject (data As Object) As Boolean
Try
Dim jstr = JsonConvert.SerializeObject(data)
Dim _invoice = JsonConvert.DeserializeObject(Of DtoInvoice)(jstr)
'Make another stuff here
Return True
Catch ex As Exception
Return False
End Try
End Function
所以我看了很多帖子,但我仍然在努力将这个 JSON 对象序列化为 classes。 JSON结构是这样的
{"value":{"HashTag":"12342345636","companyname":"my test company","LeadDetail":{"id":"1","firstname":"john","lastname":"clark","email":"emak@mai.com","phone":"9874534444"}}}
我的class结构如下:
<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/抓取Whosebug作为例子。这么多的事情,在这一点上我很慌张。所以我不断收到以下无效转换异常错误。
Conversion from type 'Dictionary(Of String,Object)' to type 'String' is not valid.
如果有人能帮助我,我将不胜感激:)
您的 类 似乎与您的 Json 不符。
使用 http://jsonutils.com/,建议您的 类 应如下所示:
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
Public Class Value
Public Property HashTag As String
Public Property companyname As String
Public Property LeadDetail As LeadDetail
End Class
Public Class LeadDetailCall
Public Property value As Value
End Class
提供的JSON只有一个元素,所以会得到一个集合(字典)。我添加了一个 "item" 以确保下面的代码有效并用于说明目的。适当的缩进使事情更容易理解:
{
"foo": {
"HashTag": "12342345636",
"companyname": "my test company",
"LeadDetail": {
"id": "1",
"firstname": "ziggy",
"lastname": "clark",
"email": "emak@mai.com",
"phone": "9874534444"
}
},
"bar": {
"HashTag": "02342345636",
"companyname": "my test company2",
"LeadDetail": {
"id": "1",
"firstname": "john",
"lastname": "clark",
"email": "emak@mai.com",
"phone": "1874534444"
}
}
}
从最深的缩进开始,很容易看到 LeadDetail
class 带有 {ID, FirstName, etc)。对于不止一项(如我的),这将重复。
然后是"foo"和"bar"对象,有一点数据和一个LeadDetail
对象。当您使用任何机器人时,它们都会为每个机器人创建一个 class。我的将被命名为 "foo" 和 "bar",但在其他方面是相同的。在下面的代码中,我将其压缩为一个名为 "Item" 的 class。然后您可以将它们(Foo 和 Bar)视为 Dictionary(of String, Item)
,其中它们的名称是键。
但是还有一个,不太明显class/Type:最外面{..}
。机器人工具将创建一个名为 "Example" 或 "RootObject" 的 class。你不需要它或想要它作为字典:
' 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 上,你会得到一本字典。
@Keith Beard 之前在这个问题中说过需要序列化,然后反序列化到 类。我从 ajax jquery 调用网络方法。
<System.Web.Services.WebMethod()>
Public Shared Function InsertObject (data As Object) As Boolean
Try
Dim jstr = JsonConvert.SerializeObject(data)
Dim _invoice = JsonConvert.DeserializeObject(Of DtoInvoice)(jstr)
'Make another stuff here
Return True
Catch ex As Exception
Return False
End Try
End Function