Reputation: 29
I have a Json Data. Trying to convert it into Datatable using Newtonsoft. But It is giving Me Error :
An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
JSon Data :
{
"items": [
{
"id": "4",
"order_id": "000000145",
"creditmemo": {
"items": [
{
"sku": "SWETA0582",
"product_id": "2640",
"item_id": "211",
"qty": "1.0000",
"reason": "Product didn’t meet your expectation."
},
{
"sku": "SWETA0583",
"product_id": "2642",
"item_id": "212",
"qty": "2.0000",
"reason": "Product didn’t meet your expectation."
}
],
"do_offline": 1,
"comment_text": "",
"shipping_amount": "49.0000",
"adjustment_positive": "",
"adjustment_negative": "49.0000"
},
"additional_remark": "",
"type": "1",
"status": "3",
"account": {
"ifsc": "PYTM0123456",
"account_no": "918981961927",
"name": "Shruti Dhandhania"
},
"refund_status": "0",
"totals_info": "",
"refund_id": "0",
"created_at": "2018-10-25 08:58:37",
"update_at": "2018-10-25 08:58:37",
"tracking_number": "",
"method": "cashondelivery",
"comment": null
},
{
"id": "5",
"order_id": "000000146",
"creditmemo": {
"items": [
{
"sku": "SWETA0584",
"product_id": "2644",
"item_id": "215",
"qty": "3.0000",
"reason": "Product didn’t meet your expectation."
},
{
"sku": "SWETA0585",
"product_id": "2646",
"item_id": "216",
"qty": "2.0000",
"reason": "Product didn’t meet your expectation."
}
],
"do_offline": 1,
"comment_text": "",
"shipping_amount": "49.0000",
"adjustment_positive": "",
"adjustment_negative": "49.0000"
},
"additional_remark": "",
"type": "1",
"status": "3",
"account": {
"ifsc": "PYTM0123456",
"account_no": "918981961927",
"name": "Shruti Dhandhania"
},
"refund_status": "0",
"totals_info": "",
"refund_id": "0",
"created_at": "2018-10-25 08:58:37",
"update_at": "2018-10-25 08:58:37",
"tracking_number": "",
"method": "cashondelivery",
"comment": null
}
]
}
Vb.Net Code :
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim dataSet = JsonConvert.DeserializeObject(Of DataSet)(TextBox1.Text)
Dim table = dataSet.Tables(0)
End Sub
Showing Error :
An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Unexpected JSON token when reading DataTable: StartObject. Path 'items[0].creditmemo', line 6, position 21.
Upvotes: 0
Views: 1439
Reputation: 29
I solved my problem.
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
Upvotes: 0
Reputation: 811
Your JSON structure is not a array of simple key value pair but is an object. For de-serializing this, you'll need to :
Do this:
YourClass yourObject = JsonConvert.DeserializeObject<YourClass>(jsonStr);
Upvotes: 1