2017-03-29 8 views
0

파일 스트림을 통해 JSON 문서 "source.json"을 읽지 만 json 파일에서 데이터를 얻는 방법은 무엇입니까? 그런 다음 새로 편집 한 json 데이터를 동일한 파일에 다시 추가하려고합니다.Filestream 및 읽기에서 데이터 받기

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 
+3

저는 휠을 다시 발명하지 않고 json.net을 사용하지 마십시오. http://www.newtonsoft.com/json/ help/html/ReadJson.htm – mxmissile

+0

NewtonSoft 패키지를 가져올 수 없다는 오류가 발생합니다. – losopha

+0

그런 다음 ['JavaScriptSerializer'] (https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx) 또는 ['DataContractJsonSerializer'] (https : // msdn. microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx). JSON은 어떤 모습입니까? – dbc

답변

1

나는 그 길을 걸어 왔습니다. newtonsoft로 잘못한 것을 알아 내려고 시도하십시오. serializer 및 deserializer 메서드는 사용하기가 매우 쉽습니다. Json 형식에 따라 객체를 생성하면 deserializer가 객체를 자동으로 채 웁니다. 클래스의 변수를 public 속성 nameofjsonobject로 선언해야합니다.