使用 Json.NET 将 JSON 反序列化为 byrefrence ParametrizedConstructor 对象

发布于 2024-10-02 14:35:56 字数 2136 浏览 4 评论 0原文

下面是我的 JSON 格式的字符串

{"AliasName": "ysiCountryInfo", "DataClass": {"Description":"United States 111","Code":"usa","WriteOffTaxPointAdjustment":0,"IndexationRounding":6} On

我想将对象反序列化到下面的类中

Option Explicit 选项严格导入

BaseApp.ysiBaseData 导入 Common.DataClass 导入系统命名

空间数据类

Public Class JSONFormatClass(Of ItemType)

    Private _Alias As String
    Public Property AliasName() As String
        Get
            Return _Alias
        End Get
        Set(ByVal value As String)
            _Alias = value
        End Set
    End Property

    Private _DataClass As ItemType
    Public Property DataClass() As ItemType
        Get
            Return _DataClass
        End Get
        Set(ByVal value As ItemType)
            _DataClass = value
        End Set
    End Property

End Class

结束命名空间

其中属性“DataClass”是“Common.DataClasses”中任何类的类型。

其中的所有类都有接受 ByRef LoginCredential 对象的参数化构造函数。

我的代码如下:

Dim loginData As New ysiLoginData()

   With loginData
     .Server = "xxxxx"
     .Platform = ServerType.SqlServer
     .Database = "xxxx"    
     .UserName = "xx"
     .Password = "xxxxx"
     .DeveloperMode = True
  End With

Dim SessionKey As New ysiSessionKey(loginData)

Dim strJSON As String = HttpUtility.UrlDecode(context.Request.Form.ToString())

Dim objJSON As JSONFormatClass(Of ysiCountryInfo) = JsonConvert .DeserializeObject(Of JSONFormatClass(Of ysiCountryInfo))(strJSON)

json 字符串格式:{"AliasName": "ysiCountryInfo", "DataClass": {"Description":"United States 111","Code":"usa"," WriteOffTaxPointAdjustment":0,"IndexationRounding":6}}

这里的“ysiCountryInfo”是我想将“DataClass”属性转换为的类类型。 “ysiCountryInfo”具有参数化构造函数,需要引用“ysiSessionKey”参数。

Dim objCountryInfo as New ysiCountryInfo(ysiSessionKey)

我在 JSON 的 JsonSerializerInternalReader.js 文件第 # 808 行处遇到错误

object createdObject = Contract.ParametrizedConstructor.Invoke(constructorParameters.Values.ToArray());

因为 constructorParameters.Values 为 Null

请帮我尽快解决这个问题。

谢谢 迪伦·米斯特里

Below is my JSON formated String

{"AliasName": "ysiCountryInfo", "DataClass": {"Description":"United States 111","Code":"usa","WriteOffTaxPointAdjustment":0,"IndexationRounding":6}}

I would like to deserialize object into below class

Option Explicit On
Option Strict On

Imports BaseApp.ysiBaseData
Imports Common.DataClasses
Imports Systems

Namespace DataClasses

Public Class JSONFormatClass(Of ItemType)

    Private _Alias As String
    Public Property AliasName() As String
        Get
            Return _Alias
        End Get
        Set(ByVal value As String)
            _Alias = value
        End Set
    End Property

    Private _DataClass As ItemType
    Public Property DataClass() As ItemType
        Get
            Return _DataClass
        End Get
        Set(ByVal value As ItemType)
            _DataClass = value
        End Set
    End Property

End Class

End Namespace

Where Property "DataClass" is type of any class from "Common.DataClasses".

And all class in this has Parametrized constructor which accept ByRef LoginCredential Object.

And My Code is below:

Dim loginData As New ysiLoginData()

   With loginData
     .Server = "xxxxx"
     .Platform = ServerType.SqlServer
     .Database = "xxxx"    
     .UserName = "xx"
     .Password = "xxxxx"
     .DeveloperMode = True
  End With

Dim SessionKey As New ysiSessionKey(loginData)

Dim strJSON As String = HttpUtility.UrlDecode(context.Request.Form.ToString())

Dim objJSON As JSONFormatClass(Of ysiCountryInfo) = JsonConvert.DeserializeObject(Of JSONFormatClass(Of ysiCountryInfo))(strJSON)

json string format : {"AliasName": "ysiCountryInfo", "DataClass": {"Description":"United States 111","Code":"usa","WriteOffTaxPointAdjustment":0,"IndexationRounding":6}}

here "ysiCountryInfo" is class type into which i would like to convert my "DataClass" property. "ysiCountryInfo" has parametrized constructor which requires parameter of "ysiSessionKey" by ref.

Dim objCountryInfo as New ysiCountryInfo(ysiSessionKey)

I am getting Error into JsonSerializerInternalReader.js file of JSON at line # 808

object createdObject = contract.ParametrizedConstructor.Invoke(constructorParameters.Values.ToArray());

Because constructorParameters.Values is Null

Please help me to solve this problem ASAP.

Thanks
Dhiren Mistry

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两个我 2024-10-09 14:35:56

抱歉,我的实施不正确。

我通过修改我的通用类 DataClass 属性解决了这个问题,如下所示。

Private _DataClass As ItemType
Public Property DataClass() As ItemType
            Get
                If _DataClass Is Nothing Then
                    Dim loginData As New ysiLoginData()
                    With loginData
                        .Server = "xxxx"
                        .Platform = ServerType.SqlServer
                        .Database = "xxx"
                        .UserName = "xx"
                        .Password = "xxx"
                        .DeveloperMode = True
                    End With

                    Dim SessionKey As New ysiSessionKey(loginData)

                    Dim args As Object() = {SessionKey}

                    _DataClass = DirectCast(Activator.CreateInstance("YSI.Common", String.Format("{0}", GetType(ItemType).ToString()), True, BindingFlags.Instance Or BindingFlags.Public, Nothing, args, Nothing, Nothing).Unwrap(), ItemType)

                End If
                Return _DataClass
            End Get
            Set(ByVal value As ItemType)
                _DataClass = value
            End Set
        End Property

Sorry i was implemented its incorrectly.

I had solve this problem by modifying my Generic Class DataClass Property As Below.

Private _DataClass As ItemType
Public Property DataClass() As ItemType
            Get
                If _DataClass Is Nothing Then
                    Dim loginData As New ysiLoginData()
                    With loginData
                        .Server = "xxxx"
                        .Platform = ServerType.SqlServer
                        .Database = "xxx"
                        .UserName = "xx"
                        .Password = "xxx"
                        .DeveloperMode = True
                    End With

                    Dim SessionKey As New ysiSessionKey(loginData)

                    Dim args As Object() = {SessionKey}

                    _DataClass = DirectCast(Activator.CreateInstance("YSI.Common", String.Format("{0}", GetType(ItemType).ToString()), True, BindingFlags.Instance Or BindingFlags.Public, Nothing, args, Nothing, Nothing).Unwrap(), ItemType)

                End If
                Return _DataClass
            End Get
            Set(ByVal value As ItemType)
                _DataClass = value
            End Set
        End Property
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文