Filestream并从读取中接收数据

时间:2017-03-29 16:25:02

标签: asp.net json vb.net filestream

通过文件流读取JSON文档“source.json”,但是如何从json文件中获取数据?之后,我试图将新编辑的json数据追加到同一个文件中。

Dim pathSource As String = "Server.MapPath('~/source.json')"
Try 
    Using fs As FileStream = New FileStream(pathSource, _
        FileMode.Open, FileAccess.Read)
            Dim bytes() As Byte = New Byte((fsSource.Length) - 1) {}
            Dim numBytesToRead As Integer = CType(fsSource.Length,Integer)
            Dim numBytesRead As Integer = 0

            While (numBytesToRead > 0)
                Dim n As Integer = fsSource.Read(bytes, numBytesRead, _
                    numBytesToRead)
                If (n = 0) Then
                    Exit While
                End If
                numBytesRead = (numBytesRead + n)
                numBytesToRead = (numBytesToRead - n)

            End While
        numBytesToRead = bytes.Length
        Dim xmlBuilder = New StringBuilder()
        fs.Seek(0, SeekOrigin.Begin)
        Dim ms As New MemoryStream()
        fs.CopyTo(ms)
        xmlBuilder.Append(Encoding.UTF8.GetString(ms.ToArray()))
        ms.Flush()
        ms.Close()
        '???How to access the data from the file "source.json" you just read in???

       'Edit the file "source.json" data 

        ?? How to put it into "bytesout" the edited data???

        Using fsAppend As FileStream = New FileStream(pathSource, _
            FileMode.Append, FileAccess.Write)
            fsAppend.Write(bytesout, 0, numBytesToRead)
        End Using
    End Using
Catch ioEx As FileNotFoundException
    Console.WriteLine(ioEx.Message)
End Try

1 个答案:

答案 0 :(得分:1)

我一直走在那条路上。试着找出你对牛顿软件做错了什么。序列化器和反序列化器方法相当容易使用。您只需按Json格式创建一个对象,反序列化器将自动填充该对象。您只需要将类中的变量声明为公共属性nameofjsonobject as