VB.net反序列化,JSON转换从类型词典“(字符串,对象)为键入'字符串' [英] VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type 'String'
问题描述
所以,我已经看过很多帖子,但我仍然有这个序列化JSON对象为类挣扎。 JSON的结构是这样的。
So I have looked at many posts, but I am still struggling with serializing this JSON object into classes. The JSON structure is this
{价值:{包括hashtag:12342345636,公司名称:我的测试company\",\"LeadDetail\":{\"id\":\"1\",\"firstname\":\"john\",\"lastname\":\"clark\",\"email\":\"emak@mai.com\",\"phone\":\"9874534444\"}}}$c$c>
我的阶级结构如下:
<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
我打电话,像这样:
I call like so:
<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/ 为例子爬行计算器。所以,很多事情,在这一点,我心慌。所以我不断得到下面的无效转换异常错误。
What have I tried? Using the JavaScriptSerializer, using http://jsontodatacontract.azurewebsites.net/ crawling stackoverflow for examples. So many things that at this point I am flustered. So I continuously get the following invalid cast exception error.
Conversion from type 'Dictionary(Of String,Object)' to type 'String' is not valid.
如果任何人能帮助我,我会非常AP preciative:)
If anyone can help me out I would be very appreciative :)
推荐答案
的JSON提供的只有一个元素,所以它会造成一个集合(字典)英寸我添加了一个项目,以确保以下工作和插图的code。正确的缩进使事情更容易执行:
The JSON provided only has one element, so it will result in a collection (dictionary) of one. I added an "item" to be sure the code below worked and for illustration purposes. Proper indentation makes things easier to follow:
{
"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"
}
}
}
最深刻缩进开始,很容易看到{ID,名字等),一个 LeadDetail
类。随着多个项目(如矿),这会重复。
Starting with the most deeply indented, it is easy to see a LeadDetail
class with {ID, FirstName, etc). With more than one item (as in mine), this would repeat.
然后是富,并一点点数据和 LeadDetail'对象栏对象。当您使用任何的机器人,他们会为每一个类。该矿将被命名为foo和酒吧,但其它是相同的。在下面的code,我这个浓缩到名为项一级。然后,你可以把他们(Foo和Bar)作为
词典(字符串,项目)',他们的姓名是键。
Then there are "foo" and "bar" objects with a little data and a LeadDetail' object. When you use any of the Robots, they will create a class for each. Mine would be named "foo" and "bar" but otherwise be identical. In the code below, I condensed this to one class named "Item". Then you can treat them (Foo and Bar) as a
Dictionary(of String, Item)` where their names are the keys.
但还有一个不太明显的类/类型:最外层的 {...}
。机器人工具将创建一个名为示例或RootObject级。你不需要它,或者想为一本字典:
But there is one more, less obvious class/Type: The outer most {..}
. The robot tools will create a class named "Example" or "RootObject". You dont need it or want it for a dictionary:
' 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
然后code:
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
输出:
键:美孚,COMPNAME:我的测试公司,联系人:齐格结果
键:酒吧,COMPNAME:我的测试Company2的,联系:John
key: foo, CompName: my test company, Contact: ziggy
key: bar, CompName: my test company2, Contact: john
如果您在原来的JSON运行它,你就会得到一个字典。
If you run it on your original JSON, you get a dictionary of one.
这篇关于VB.net反序列化,JSON转换从类型词典“(字符串,对象)为键入'字符串'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!