1

I have a 256 size buffer which receives data and "stores" them as a byte array. The data contained are JSON Objects, but I can't figure out how to manipulate them. The worst thing I have to deal with is that the JSON objects are stacked. 1st JSON Object contains more than one JSON Objects and every JSON Object contains another one. First byte array contains the first object and the last byte array contains the closing bracket of that first object.

How am I supposed to get the data I want and manipulate them further on? I want to save every object in a SQL database after separating the JSON Objects. Would it be better to implement a list or an array to save the byte arrays or is there a better solution?

Mikael Dúi Bolinder
  • 2,080
  • 2
  • 19
  • 44
Rapsoulis
  • 232
  • 5
  • 13
  • 2
    Why do you store json as a `byte[]` instead of a string or custom objects? – René Vogt Feb 22 '17 at 08:21
  • Normally if can get whole JSON, You can insert them as classes and use OOP approach – Tatranskymedved Feb 22 '17 at 08:22
  • Simple google search will give you a lot of .NET JSON library options. – Gordon Feb 22 '17 at 08:23
  • http://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c – Tatranskymedved Feb 22 '17 at 08:24
  • How is that buffer filled? Would a [Stream](https://msdn.microsoft.com/en-us/library/k3352a4t.aspx)-based approach be possible? – Hans Kesting Feb 22 '17 at 08:31
  • @RenéVogt I store them as byte[] because I receive the data via websocket and all of them at once. Also, as I mentioned (but maybe didn't was clear) the objects are stacked. Which means the first object contains all other objects and closes in the last byte array received. Imagine something like this: {obj: {obj: {[obj} } }. – Rapsoulis Feb 22 '17 at 08:34
  • @HansKesting 'byte[] buffer = new byte[receiveChunkSize]; while (webSocket.State == System.Net.WebSockets.WebSocketState.Open) { var result = await webSocket.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None); .... ... – Rapsoulis Feb 22 '17 at 08:47

2 Answers2

1

First convert the byte array to string. This also depends on encoding. Now you should get a normal JSON represented as a string object. You can make sure that your JSON looks "good" now just simply by looking at the string variable and what it contains, which would not have been so easy with the byte array you had in the beginning.

Afterwards, you can try and convert the JSON string from above step to objects.

Community
  • 1
  • 1
Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • If I convert each byte array to string, I'm going to have multiple strings, since my data are more than a string can hold (if I'm correct a string value can hold up to 256 characters). – Rapsoulis Feb 22 '17 at 08:40
  • @Rapsoulhs String can hold *more* than 256 characters – Giorgi Moniava Feb 22 '17 at 08:45
  • @Rapsoulhs JSON is string. If you want to send it to server, convert to byte array and send through socket. On the receiver and parse whole byte array again convert to string and then use the last link from my answer. – Giorgi Moniava Feb 22 '17 at 08:56
0

Say the byte array is a string encoded with UTF-8, use Encoding.GetString(byte[] bytes) to make it a string.

byte[] jsonBytes = GetJsonBytes();

string jsonString = Encoding.UTF8.GetString(jsonBytes);

Now you can use, for example, Newtonsoft.Json to convert it to a DTO type.

MyDto myDtoObj = JsonConvert.DeserializeObject<MyDto>(jsonString);
Mikael Dúi Bolinder
  • 2,080
  • 2
  • 19
  • 44