2015-11-19 88 views
1

我有這個類對象中的字節數組場:我得到的錯誤,包括一個JSON對象

public class Gallery 
{ 
    public int Id { get; set; } 
    public bool IsUploaded { get; set; } 
    public string Title { get; set; } 
    public string Description { get; set; } 
    public byte[] Image; 
    public string JobRef { get; set; } 
} 

我創建它的一個實例,並使用序列化牛頓JSON:

var json = JsonConvert.SerializeObject('the object'); 

然後我這樣做的同時轉換爲字節數組,並將其發送我的插座聽另一臺PC上:

byte[] byteArray = Encoding.UTF8.GetBytes(json); 

using (NetworkStream serverStream = clientSocket.GetStream()) 
{ 
    serverStream.Write(byteArray, 0, byteArray.Length); 
    //more code here but not relevant as the error happens on listening PC 
} 

在我的聽力P C I收到JSON像這樣:

using (IInputStream input = args.Socket.InputStream) 
{ 
    byte[] data = new byte[BufferSize]; 
    IBuffer buffer = data.AsBuffer(); 
    uint dataRead = BufferSize; 
    while (dataRead == BufferSize) 
    { 
     await input.ReadAsync(buffer, BufferSize, InputStreamOptions.Partial); 
     request.Append(Encoding.UTF8.GetString(data, 0, data.Length)); 
     dataRead = buffer.Length; 
    } 
} 

var job = JsonConvert.DeserializeObject<Gallery>(request.ToString()); 

在最後一行我得到的錯誤:

Additional text encountered after finished reading JSON content: r. Path '', line 34, position 2. 

但是,如果我刪除字節數組圖像沒有錯誤。那麼,是否有一種方法可以將圖像數組與json對象一起使用?

謝謝

回答

3

Json不支持字節數組。您需要將其轉換爲Base64。請參閱this線程,還有this one

相關問題