【发布时间】:2019-02-11 17:34:33
【问题描述】:
我有一个 Json 数据。尝试使用 Newtonsoft 将其转换为 Datatable。但它给了我错误:
“Newtonsoft.Json.JsonSerializationException”类型的异常 发生在 Newtonsoft.Json.dll 中但未在用户代码中处理
JSON 数据:
{
"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 代码:
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
显示错误:
“Newtonsoft.Json.JsonSerializationException”类型的异常 发生在 Newtonsoft.Json.dll 中但未在用户代码中处理
附加信息:读取 DataTable 时出现意外的 JSON 令牌: 起始对象。路径 'items[0].creditmemo',第 6 行,位置 21。
【问题讨论】:
-
嗯:我在 Notepad++ 中查看了您的 JSON,并使用 JSLint (jslint.com) 对其进行了验证 - 这很好。所以听起来JSON和“数据集”之间存在一些差异。问:您能告诉我们您在哪里定义了具有“creditmemo”的数据结构吗?
-
显示 JSON 良好
-
这就是我所说的:Notepad++ 和 JSLint 都表明 JSON 是好的。这意味着问题可能出在 VB.Net 方面。请更新您的帖子并向我们展示包含“creditmemo”的VB.Net“数据结构”;您用于“数据集”的那个。向我们展示您定义数据集的代码。
-
我没有定义任何数据结构