Newtonsoft.Json.dll中发生类型为'Newtonsoft.Json.JsonSerializationException'的异常,但未在用户代码中处理

时间:2019-02-11 17:34:33

标签: json vb.net datatable

我有一个Json数据。尝试使用Newtonsoft将其转换为Datatable。但这给了我错误:

  

“ Newtonsoft.Json.JsonSerializationException”类型的异常   发生在Newtonsoft.Json.dll中,但未在用户代码中处理

JSon数据:

SHFileOperation

Vb.Net代码:

IFileOperation

显示错误:

  

“ Newtonsoft.Json.JsonSerializationException”类型的异常   发生在Newtonsoft.Json.dll中,但未在用户代码中处理

     

其他信息:读取DataTable时出现意外的JSON令牌:   StartObject。路径“ items [0] .creditmemo”,第6行,位置21。

2 个答案:

答案 0 :(得分:1)

您的JSON结构不是简单键值对的数组,而是一个对象。要对此进行反序列化,您需要:

  1. 为反序列化数据创建一个
  2. 执行此操作:

      YourClass yourObject = JsonConvert.DeserializeObject<YourClass>(jsonStr);
    

答案 1 :(得分:0)

我解决了我的问题。

Partial Class Default2
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(sender As Object, e As EventArgs)


            Dim js As JavaScriptSerializer = New JavaScriptSerializer()
            Dim response As RootObject = js.Deserialize(Of RootObject)(txtJsonData.Text)

            Dim dset = New DataSet()

            AddToDataSet(dset, response, Nothing, Nothing)

            For Each itm In response.items
                AddToDataSet(dset, itm.account, itm.order_id, "order_id")
            Next
            For Each itm In response.items
                AddToDataSet(dset, itm.creditmemo, itm.order_id, "order_id")
            Next

            Dim dt As DataSet = dset


        End Sub


        Public Sub AddToDataSet(ByVal dset As DataSet, ByVal value As Object, ByVal strprimaryColValue As String, ByVal primaryColName As String)
            'If dset Is Nothing Then Throw New ArgumentNullException(NameOf(dset))
            Dim mprimaryColValue As String
            Dim mprimaryColName As String
            mprimaryColValue = strprimaryColValue
            mprimaryColName = primaryColName

            If value Is Nothing Then Return
            Dim type = value.[GetType]()
            Dim table = dset.Tables(type.FullName)

            If table Is Nothing Then
                table = New DataTable(type.FullName)
                dset.Tables.Add(table)

                For Each prop In type.GetProperties().Where(Function(p) p.CanRead)
                    If IsEnumerable(prop) Then Continue For
                    Dim col = New DataColumn(prop.Name, prop.PropertyType)
                    table.Columns.Add(col)

                    If strprimaryColValue IsNot Nothing Then
                        If Not table.Columns.Contains(primaryColName) Then
                            table.Columns.Add(primaryColName)
                        End If
                    End If
                Next
            End If

            Dim row = table.NewRow()

            For Each prop In type.GetProperties().Where(Function(p) p.CanRead)
                Dim propValue As Object = prop.GetValue(value)

                If IsEnumerable(prop) Then

                    If propValue IsNot Nothing Then

                        For Each child In CType(propValue, ICollection)

                            AddToDataSet(dset, child, mprimaryColValue, mprimaryColName)

                        Next
                    End If

                    Continue For
                End If

                row(prop.Name) = propValue

                If strprimaryColValue IsNot Nothing Then
                    If table.Columns.Contains(primaryColName) Then
                        row(primaryColName) = strprimaryColValue
                    End If
                End If

            Next

            table.Rows.Add(row)
        End Sub

        Private Function IsEnumerable(ByVal pi As PropertyInfo) As Boolean
            Return GetType(ICollection).IsAssignableFrom(pi.PropertyType)
        End Function
    End Class


    Public Class Products
        Public Property sku As String
        Public Property product_id As String
        Public Property item_id As String
        Public Property qty As String
        Public Property reason As String
    End Class

    Public Class Creditmemo
        Public Property items As List(Of Products)
        Public Property do_offline As Integer
        Public Property comment_text As String
        Public Property shipping_amount As String
        Public Property adjustment_positive As String
        Public Property adjustment_negative As String
    End Class

    Public Class Account
        Public Property ifsc As String
        Public Property account_no As String
        Public Property name As String
    End Class

    Public Class Orders
        Public Property id As String
        Public Property order_id As String
        Public Property creditmemo As Creditmemo
        Public Property additional_remark As String
        Public Property type As String
        Public Property status As String
        Public Property account As Account
        Public Property refund_status As String
        Public Property totals_info As String
        Public Property refund_id As String
        Public Property created_at As String
        Public Property update_at As String
        Public Property tracking_number As String
        Public Property method As String
        Public Property comment As Object
    End Class

    Public Class RootObject
        Public Property total_size As Integer
        Public Property items As List(Of Orders)
    End Class