我想将一个大的JSON文件(〜600MB)反序列化为DataGridView,而不会遇到“ maxJsonLength”错误。
“我的代码”适用于小型JSON文件。但是对于更大的JSON文件,我会收到“ maxJsonLength”错误。由于我不是最有经验的编码员,是否有解决方法或简单的方法来解决此问题?
Private Sub BtnOpenFile_Click(sender As Object, e As EventArgs) Handles BtnOpenFileOld.Click
OpenFileDialog1.InitialDirectory = "C:\"
If OpenFileDialog1.ShowDialog = DialogResult.Cancel Then
End If
Dim JSonFilePath As String = File.ReadAllText(OpenFileDialog1.FileName)
LblFilePath.Text = OpenFileDialog1.FileName
DataGridView1.Rows.Clear()
DataGridView1.Refresh()
Dim dict As Object = New JavaScriptSerializer().Deserialize(Of List(Of Object))(JSonFilePath)
For Each item As Object In dict
DataGridView1.Rows.Add(item("EMail").ToString, item("Timestamp").ToString, item("Number").ToString)
Next
End Sub
我的JSON文件看起来像
[
{
"EMail": "one@mail.com",
"Timestamp": "2019-05-25T21:24:06.799381+02:00",
"Number": 206074,
"randomtrash1": "notneeded",
"randomtrash2": "notneeded",
"randomtrash3": "notneeded",
"randomtrash4": "notneeded",
"randomtrash5": "notneeded",
"randomtrash6": "notneeded",
"randomtrash7": "notneeded",
"randomtrash8": "notneeded",
"randomtrash9": "notneeded"
},
{
"EMail": "two@mail.com",
"Timestamp": "2019-05-25T21:24:06.8273826+02:00",
"Number": 7397,
"randomtrash1": "notneeded",
"randomtrash2": "notneeded",
"randomtrash3": "notneeded",
"randomtrash4": "notneeded",
"randomtrash5": "notneeded",
"randomtrash6": "notneeded",
"randomtrash7": "notneeded",
"randomtrash8": "notneeded",
"randomtrash9": "notneeded",
"randomtrash10": "notneeded",
"randomtrash11": "notneeded",
"randomtrash12": "notneeded",
"randomtrash13": "notneeded",
"randomtrash14": "notneeded",
"randomtrash15": "notneeded",
"randomtrash16": "notneeded",
"randomtrash17": "notneeded",
"randomtrash18": "notneeded",
"randomtrash19": "notneeded",
"randomtrash20": "notneeded",
"randomtrash21": "notneeded"
}
]
答案 0 :(得分:0)
避免maxJsonLength
错误的最小解决方法是将JavaScriptSerializer.MaxJsonLength = int.MaxValue
所示的this answer设置为The length of the string exceeds the value set on the maxJsonLength property的 Taha Rehman Siddiqui :
Dim serializer As JavaScriptSerializer = New JavaScriptSerializer()
serializer.MaxJsonLength = Int32.MaxValue
Dim dict As Object = serializer.Deserialize(Of List(Of Object))(JSonFilePath)
但是,您当前正在将600MB文件加载到600-1,200MB字符串中,该字符串正在接近.Net maximum string length。如果文件变大,您将开始遇到内存不足异常。更好的方法是使用流传输解决方案直接从文件中反序列化,但是JavaScriptSerializer
不支持流传输。因此,我建议切换到json.net。
首先,如图here所示安装Json.NET。接下来,由于您似乎不想为JSON定义显式数据模型,因此请创建以下静态方法:
Public Module JsonExtensions
Public Function LoadAnonymousType(Of T)(ByVal path as String, ByVal anonymousTypeObject as T, Optional ByVal settings as JsonSerializerSettings = Nothing) as T
Using streamReader As TextReader = File.OpenText(path)
Dim serializer = JsonSerializer.CreateDefault(settings)
return CType(serializer.Deserialize(streamreader, GetType(T)), T)
End Using
End Function
End Module
这是将由path
参数指定位置的文件的内容反序列化为anonymousTypeObject
参数指定类型的数据模型。
现在,您将能够直接将JSON从文件反序列化为anonymous type objects的数组,如下所示:
Dim array = JsonExtensions.LoadAnonymousType( _
FileName, _
{ New With {.Email = CType(Nothing, String), .Timestamp = CType(Nothing, String), .Number = CType(Nothing, Long) }} _
)
For Each item In array
' DataGridView1.Rows.Add(item("EMail").ToString, item("Timestamp").ToString, item("Number").ToString)
Console.WriteLine("Email = {0}, Timestamp = {1}, Number = {2}", item.Email, item.Timestamp, item.Number)
Next
或者,您可以创建一个明确的数据模型,如下所示:
Public Class RootObject
Public Property Email As String
Public Property Timestamp As String
Public Property Number As Long
End Class
并反序列化,如 Deserialize JSON from a file 中所示:
Dim list As List(Of RootObject) = Nothing
Using streamReader As TextReader = File.OpenText(FileName)
list = CType(JsonSerializer.CreateDefault().Deserialize(streamReader, GetType(List(Of RootObject))), List(Of RootObject))
End Using
For Each item In list
' DataGridView1.Rows.Add(item("EMail").ToString, item("Timestamp").ToString, item("Number").ToString)
Console.WriteLine("Email = {0}, Timestamp = {1}, Number = {2}", item.Email, item.Timestamp, item.Number)
Next
演示小提琴here。
答案 1 :(得分:0)
Imports Newtonsoft.Json
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim str As String = _
"[" + _
" {" + _
" ""EMail"": ""one@mail.com""," + _
" ""Timestamp"": ""2019-05-25T21:24:06.799381+02:00""," + _
" ""Number"": 206074," + _
" }," + _
" {" + _
" ""EMail"": ""two@mail.com""," + _
" ""Timestamp"": ""2019-05-25T21:24:06.8273826+02:00""," + _
" ""Number"": 7397," + _
" }," + _
"]"
Try
Dim list As List(Of jsnn) = JsonConvert.DeserializeObject(Of List(Of jsnn))(str)
For Each item As jsnn In list
Console.WriteLine(item.EMail & ": " & item.Timestamp & ": " & item.Number)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
Public Class jsnn
Public Property EMail As String
Public Property Timestamp As DateTime
Public Property Number As Integer
End Class