VB.net 反序列化,JSON 从类型“Dictionary(Of String,Object)”到“String”类型的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29849015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type 'String'
提问by Keith Beard
So I have looked at many posts, but I am still struggling with serializing this JSON object into classes. The JSON structure is this
所以我看了很多帖子,但我仍然在努力将这个 JSON 对象序列化为类。JSON 结构是这样的
{"value":{"HashTag":"12342345636","companyname":"my test company","LeadDetail":{"id":"1","firstname":"john","lastname":"clark","email":"[email protected]","phone":"9874534444"}}}
{"value":{"HashTag":"12342345636","companyname":"my test company","LeadDetail":{"id":"1","firstname":"john","lastname":"clark","email":"[email protected]","phone":"9874534444"}}}
My class structure is as follows:
我的班级结构如下:
<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
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.
我试过什么?使用 JavaScriptSerializer,使用http://jsontodatacontract.azurewebsites.net/抓取 stackoverflow 作为示例。太多的事情,在这一点上我心慌意乱。所以我不断收到以下无效转换异常错误。
Conversion from type 'Dictionary(Of String,Object)' to type 'String' is not valid.
If anyone can help me out I would be very appreciative :)
如果有人可以帮助我,我将不胜感激:)
采纳答案by ??ssa P?ngj?rdenlarp
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:
提供的 JSON 只有一个元素,所以它会产生一个集合(字典)。我添加了一个“项目”以确保下面的代码有效并用于说明目的。适当的缩进使事情更容易理解:
{
"foo": {
"HashTag": "12342345636",
"companyname": "my test company",
"LeadDetail": {
"id": "1",
"firstname": "ziggy",
"lastname": "clark",
"email": "[email protected]",
"phone": "9874534444"
}
},
"bar": {
"HashTag": "02342345636",
"companyname": "my test company2",
"LeadDetail": {
"id": "1",
"firstname": "john",
"lastname": "clark",
"email": "[email protected]",
"phone": "1874534444"
}
}
}
Starting with the most deeply indented, it is easy to see a LeadDetailclass with {ID, FirstName, etc). With more than one item (as in mine), this would repeat.
从缩进最深的开始,很容易看到LeadDetail带有 {ID、FirstName 等)的类。如果有多个项目(如我的),这会重复。
Then there are "foo" and "bar" objects with a little data and a LeadDetailobject. 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.
然后是带有少量数据和LeadDetail对象的“foo”和“bar”对象。当您使用任何机器人时,它们都会为每个机器人创建一个类。我的将被命名为“foo”和“bar”,但在其他方面是相同的。在下面的代码中,我将其压缩为一个名为“Item”的类。然后您可以将它们(Foo 和 Bar)视为Dictionary(of String, Item)它们的名称是键的地方。
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:
但是还有一个不那么明显的类/类型:最外面的{..}. 机器人工具将创建一个名为“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
Then the 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
Output:
输出:
key: foo, CompName: my test company, Contact: ziggy
key: bar, CompName: my test company2, Contact: john
密钥:foo,CompName:我的测试公司,联系人:ziggy
密钥:bar,CompName:我的测试公司 2,联系人:john
If you run it on your original JSON, you get a dictionary of one.
如果在原始 JSON 上运行它,则会得到一个字典。
回答by Hans Olsson
Your classes don't seem to correspond properly to your Json.
您的课程似乎与您的 Json 不正确。
Using http://jsonutils.com/, it suggested that your classes should look like this:
使用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

