0

Reading an JSON document "source.json" thru filestream but how do you get the data from the json file? After that I am trying to append the newly edited json data back on the same file.

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
losopha
  • 127
  • 1
  • 3
  • 10
  • 3
    I'd avoid re-inventing the wheel like that and just use json.net: http://www.newtonsoft.com/json/help/html/ReadJson.htm – mxmissile Mar 29 '17 at 16:27
  • I cannot get NewtonSoft the package gives an error. – losopha Mar 29 '17 at 16:32
  • Then use [`JavaScriptSerializer`](https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx) or [`DataContractJsonSerializer`](https://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx). What does the JSON look like? – dbc Mar 29 '17 at 20:01
  • If you are asking, "How can I deserialize JSON in .Net", see [How can I parse JSON with C#?](https://stackoverflow.com/q/6620165/3744182) or [VB.net JSON Deserialize](https://stackoverflow.com/q/8118019/3744182) or any of the other top-voted questions tagged with [JSON and VB.NET](https://stackoverflow.com/questions/tagged/json+vb.net?sort=votes&pageSize=50). Otherwise, please ask a more specific question. – dbc Mar 29 '17 at 20:08

1 Answers1

1

I've been down that path. Try to figure out what you did wrong with newtonsoft. The serializer and deserializer methods are fairly easy to use. You only have to create an object per the Json format and deserializer will populate the object automatically. You just have to have the variables in the class be declared as public property nameofjsonobject as

Ralph Maurmeier
  • 101
  • 1
  • 11