VB.net反序列化,JSON转换类型'字典(字符串,对象)'输入' String'

时间:2015-04-24 13:38:25

标签: asp.net json vb.net web-services serialization

所以我看了很多帖子,但我仍然在努力将这个JSON对象序列化为类。 JSON结构就是这个

{"value":{"HashTag":"12342345636","companyname":"my test company","LeadDetail":{"id":"1","firstname":"john","lastname":"clark","email":"emak@mai.com","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.

如果有人能帮助我,我会非常感激:)

3 个答案:

答案 0 :(得分:2)

提供的JSON只有一个元素,因此它将产生一个集合(字典)。我添加了一个&#34;项目&#34;确保下面的代码工作并用于说明目的。适当的缩进使事情更容易理解:

{
    "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类具有{ID,FirstName等)。有多个项目(如我的),这将重复。

然后有&#34; foo&#34;和&#34; bar&#34;具有少量数据和LeadDetail对象的对象。当您使用任何机器人时,他们将为每个机器人创建一个类。我的名字将被命名为#f;&#34;和&#34; bar&#34;但除此之外是相同的。在下面的代码中,我将其浓缩为一个名为&#34; Item&#34;的类。然后你可以将它们(Foo和Bar)视为Dictionary(of String, Item),其名称是键。

但还有一个不太明显的类/类型:最外层的{..}。机器人工具将创建一个名为&#34;示例&#34;或&#34; RootObject&#34;。你不需要它或想要它用于字典:

' 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:我的测试公司,联系方式:ziggy
  key:bar,CompName:我的测试公司2,联系人:john

如果您在原始JSON上运行它,则会得到一个字典。

答案 1 :(得分:0)

你的课程似乎与你的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

答案 2 :(得分:0)

@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